今天我在自定义类重载运算符<<时遇到了一个很奇怪的报错......
先上最后能够正常运行的代码吧(相关头文件在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;
}
是一个很简单的重载<<运算符的函数,但是我是看博客照猫画虎搞下来的。
friend,&,const这些大概有什么用我理解,但是具体为什么这么写我不是很清楚。所以不理解后续的各种问题。
(1)删掉

最低0.47元/天 解锁文章
1651

被折叠的 条评论
为什么被折叠?



