我正在研究一个小的直方图类,有些东西困扰着我。这是代码:
class cHistogram{
public:
cHistogram();
cHistogram(int x);
friend ostream& operator<<(ostream& ost, cHistogram& obj);
int operator()(const unsigned int n){return niz[n];};
int operator [](const unsigned int n){return niz[n]++;};
private:
int niz[100];
int total;
};
int main() {
cHistogram h(100);
while(true){
unsigned int n;
std::cin >> n;
if(0 == n) break;
h[n]++;
}
std::cout << h;
}
问题是在尝试编译时,它给了我h[n]
必须是一个可修改的左值。我不太明白我应该在这里做什么......欢迎任何意见!:)