编译器抛出:“无效使用非静态成员'it',这是为什么?继承是正确的,但是我不明白为什么它不让我使用它和allInfo向量。
class JSON{
private:
vector<myType> allInfo;
public:
friend ostream &operator<<(ostream &os,const JSON &js)
{
vector<myType>::iterator it;
it = this->allInfo.begin();
for(it; it != allInfo.end();it++){
cout << "this is the info "<<(it->getNAME()) << endl;
}
return os;
};
};
operator <<是一个friend
函数,实际上不是该类的成员JSON
。因此,如果您只是说allInfo
,则编译器不知道allInfo
您在说什么。
但是,正确的JSON
实例作为参数传递。您应该这样写:
it = js.allInfo.begin();
/* ... */
for(it; it != js.allInfo.end();it++){
现在,您要告诉编译器您要使用allInfo
属于instance的js
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句