2016-12-18 71 views
-1

編譯器拋出:「非法使用非靜態成員'它',爲什麼會這樣? 繼承是正確的,但我不明白爲什麼它不允許我使用它和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; 
    }; 

}; 
+3

你想'js.allInfo'(和'const_iterator')。 'operator <<'這裏不是'JSON'的成員;爲了訪問'JSON'的成員,你需要指定你想從中獲得它們的對象。幸運的是,一個參數很容易被傳遞。 –

回答

3

操作< <是friend功能,它實際上並不是類JSON的成員。因此,如果你只說allInfo,編譯器不知道什麼allInfo你在談論。

然而,相關將ct JSON實例作爲參數傳遞。你應該寫行是這樣的:

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

現在,你告訴你要使用屬於實例jsallInfo編譯器。

相關問題