今天我在自定义类重载运算符<<时遇到了一个很奇怪的报错......
先上最后能够正常运行的代码吧(相关头文件在stdafx.h里面,懒得打了)
// point.h
class point
{
public:
point(int x, int y);
string printPos(); // "[3,5]"
friend ostream &operator<<(ostream &os, const point &p); // [3,5]
private:
int X, Y;
};
// point.cpp
#include "stdafx.h"
#include "point.h"
point::point(int x, int y)
{
X = x;
Y = y;
}
string point::printPos() // "[3,5]"
{
return "[" + to_string(X) + "," + to_string(Y) + "]";
}
ostream &operator<<(ostream &os, const point &p) // [3,5]
{
os << "[" + to_string(p.X) + "," + to_string(p.Y) + "]" << endl;
return os;
}
// ConsoleApplication2.cpp
#include "stdafx.h"
#include "point.h"
int main()
{
point p(3, 5);
cout << p;
return 0;
}