简体   繁体   中英

error: invalid use of non-static member

The compiler throws: "invalid use of non-static member 'it', why is this ? The inheritance is correct but I dont understand why it doesnt let me use it and the allInfo vector.

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<< is a friend function, it's not actually a member of the class JSON . Therefore, if you just say allInfo , the compiler doesn't know what allInfo you're talking about.

However, the correct JSON instance is passed as a parameter. You should write lines like this:

it = js.allInfo.begin();
/* ... */
for(it; it != js.allInfo.end();it++){

Now, you're telling the compiler you want to use the allInfo that belongs to instance js .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM