2016-03-06 126 views
1

我有一個類圖與構造函數和重載operator <<graph.h錯誤操作符<<重載 - 沒有操作員發現

class Graph 
{ 
    private: 
     vector<int> setOfVertices; 
    public: 
     Graph(ifstream &);  //konstruktor ze souboru 
     friend ofstream & operator<<(ofstream&, const Graph &); 
}; 

構造函數的定義(小例子,並不重要)和運營商< <在分隔文件graph.cpp

ofstream & operator<<(ofstream& outputStream, const Graph & graphToPrint) 
{ 
    //not important for minimal example 
    return outputStream; 
} 

當我打電話operator <<main.cpp

#include <iostream> 
#include <fstream> 
#include "graph.h" 

using namespace std; 

int main() 
{ 
    ifstream myFile ("example.txt"); 
    Graph * G = new Graph(myFile); 
    cout << *G; 
    return 0; 
} 

我得到一個錯誤

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Graph' (or there is no acceptable conversion) 

我沒管理由自己來定位代碼的錯誤,我會感謝所有的建議。

回答

1

std::cout是類型std::ostream的全局對象而不是std::ofstreamstd::ofstreamstd::ostream的衍生產品。見http://en.cppreference.com/w/cpp/io/cout

因此,修改你的朋友的功能(操作員)到

friend ostream & operator<<(ostream&, const Graph &); 
相關問題