2015-05-22 219 views
1

我想超載運算符< <但我一直有這個錯誤。我試着做研究,但沒有結果。我有一個Point2D.h和一個Point2D.cpp與一個朋友函數重載。下面是我的代碼:C++朋友重載運算符<<

Point2D.h

#include <string> 
#include <iomanip> 

using namespace std; 

#ifndef Point2D_H 
#define Point2D_H 

class Point2D 
{ 
    friend ostream& operator<< (ostream&, Point2D); 

    public: 

     Point2D(); 

     Point2D(int, int); 

    protected: 

     int x; 
     int y; 
}; 

Point.cpp

#include <string> 
#include <cmath> 
#include <iomanip> 
#include "Point2D.h" 

Point2D::Point2D() { 
    this->x=0; 
    this->y=0; 
} 

Point2D::Point2D(int x, int y) { 
    this->x=x; 
    this->y=y; 
} 
ostream& operator<< (ostream &out, Point2D pt) 
{ 
    out << "Point = " <<pt.x; 
    return out; 
} 
#endif 

下面是我的錯誤信息,不知道爲什麼沒有匹配該方法

Point2D.h: In function ‘std::ostream& operator<<(std::ostream&, Point2D)’: 
Point2D.h:37:9: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘int’) 
    out << pt.x; 
     ^
Point2D.h:37:9: note: candidates are: 
Point2D.h:35:10: note: std::ostream& operator<<(std::ostream&, Point2D) 
ostream& operator<< (ostream &out, Point2D pt) 
     ^
Point2D.h:35:10: note: no known conversion for argument 2 from ‘int’ to ‘Point2D’ 
In file included from Point2D.h:2:0, 
       from Point3D.h:2, 
       from Point3D.cpp:2: 
/usr/include/c++/4.8/iomanip:235:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::_Setw) 
operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f) 

回答

3

您需要

#include <iostream> 

或者至少

#include <ostream> 

其他2建議:

  • 的包括防護裝置(ifndefdefineendif)應該是在開始的時候,在的盡頭頭文件文件(絕不能在源文件中,但在頭文件中)
  • 在標題中添加using namespace std;是不好的做法。至少在標題中使用std::前綴。如果您在源文件中使用using namespace std;,則是您的選擇。我不會,但這是我個人的選擇。
0

您需要包括另一頭

#include <iostream> 

只有#include <ostream>會雖然足夠了。

0

正確的代碼(見代碼中的註釋):

Point2D.h:

#include <string> 
// #include <iomanip> // this includes io manipulators you do not need here 
#include <iosfwd> // minimalist forward declarations for io streams 

// using namespace std; // don't do this :(

#ifndef Point2D_H // should be above the includes 
#define Point2D_H // should be above the includes 

class Point2D 
{ 
    // friend ostream& operator<< (ostream&, Point2D); 
    friend std::ostream& operator<< (std::ostream&, const Point2D &); 
    // observe pass by const reference 

Point2D.cpp

#include <string> 
#include <cmath> 
// #include <iomanip> // not needed 
#include <iostream> // std::ostream class definition 
#include "Point2D.h" 

Point2D::Point2D() { 
    this->x=0; 
    this->y=0; 
} 

Point2D::Point2D(int x, int y) { 
    this->x=x; 
    this->y=y; 
} 
// ostream& operator<< (ostream &out, Point2D pt) 
std::ostream& operator<< (ostream &out, const Point2D& pt) 
{ 
    out << "Point = " << pt.x; 
    return out; 
} 
// #endif // this shouldn't be here 
0

首先移動指令

#endif 

的從文件Point.cpp到文件末尾Point2D.h 這些文件看起來像

Point2D.h

#include <string> 
#include <iomanip> 

using namespace std; 

#ifndef Point2D_H 
#define Point2D_H 

//... 

#endif 

點。CPP

#include <string> 
#include <cmath> 
#include <iomanip> 
#include "Point2D.h" 

Point2D::Point2D() { 
    this->x=0; 
    this->y=0; 
} 

Point2D::Point2D(int x, int y) { 
    this->x=x; 
    this->y=y; 
} 
ostream& operator<< (ostream &out, Point2D pt) 
{ 
    out << "Point = " <<pt.x; 
    return out; 
} 
// #endif - removed 

並替換

#include <iomanip> 

#include <iostream> 

或至少添加報頭

#include <iostream> 

到操作者所定義的模塊文件。

而且操作員應當操作者將被用於將被創建類型Point2D的臨時對象每次聲明如下

ostream& operator<< (ostream &out, const Point2D &pt); 

否則。