我对朋友操作符重载感到困惑。如果我在头文件中编写friend运算符重载函数,它没有问题,但是一旦将函数移动到类文件,它会给我以下错误。我搜索了一些样本,他们都在头文件中写了函数。我做错了什么?谢谢。C++的朋友操作符+超载
...: error: expected ‘,’ or ‘...’ before ‘&’ token
...: error: ISO C++ forbids declaration of ‘statisticain’ with no type
...: error: ‘main_savitch_2C::statistician operator+(int)’ must have an argument of class or enumerated type
// a.h
class A
{
public:
friend A operator + (const A &a1, const A &a2);
};
// a.cpp
#include "a.h"
A operator + (const A &a1, const A &a2)
{
//
}
该代码适用于我。你的**实际**代码是什么样的? – 2011-02-09 06:19:04
因为每个人都给出了关于如何正确地重载operator +的建议,[这里是](http://codepad.org/8E9m5A7a)我的建议。 – 2011-02-09 06:58:12