1
我已經定義了一個使用dev C++的點類。然後我試圖爲這個班級重載cout。 雖然沒有使用它,我沒有得到任何錯誤。但是當我在主要使用它,它給了我這個錯誤:undefined引用,而重載cout
[Linker error] C:\Users\Mohammad\Desktop\AP-New folder\point/main.cpp:12: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Point const&)'
//point.h
class Point{
private:
double x;
double y;
double z;
public:
//constructors:
Point()
{
x=0;
y=0;
z=0;
}
Point(double xx,double yy,double zz){x=xx; y=yy; z=zz;}
//get:
double get_x(){return x;}
double get_y(){return y;}
double get_z(){return z;}
//set:
void set_point(double xx, double yy, double zz){x=xx; y=yy; z=zz;}
friend ostream &operator<<(ostream&,Point&);
};
//point.cpp
ostream &operator<<(ostream &out,Point &p){
out<<"("<<p.x<<", "<<p.y<<", "<<p.z<<")\n";
return out;
}
//main.cpp
#include <iostream>
#include "point.h"
using namespace std;
int main(){
Point O;
cout<<"O"<<O;
cin.get();
return 0;
}
它似乎是答案 –
但在learncpp.com它沒有使用const! –
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/ –