C ++运算符重载错误

时间:2016-11-22 13:18:46

标签: c++ overloading operator-keyword

我正在尝试编写一个基本的运算符重载代码。但是,我收到一些我无法解释的错误。

头文件:

class Overloading
{
public:
    int number;
    Overloading();
    Overloading(int);
    Overloading operator+(Overloading);
    void vUselessoverloadFunction();
};

C ++代码:

Overloading::Overloading()
{}

Overloading::Overloading(int i)
{
    number = i;
}

Overloading Overloading::operator+(Overloading Object2)
{
    Overloading NewObject;
    NewObject.number = number + Object2.number;
    return (NewObject);
}
void Overloading::vUselessoverloadFunction() {

Overloading ObjectOne(15);
Overloading ObjectTwo(24);
Overloading ObjectThree();

ObjectThree = ObjectOne+ObjectTwo;
cout << ObjectOne.number << endl;
cout << ObjectTwo.number << endl;
cout << "Overloaded object value: " << ObjectThree.number << endl;

}

我收到以下错误:
在线ObjectThree = ObjectOne+ObjectTwo;
- 功能分配&#39;重载ObjectThree()&#39;
- 无法转换&#39;重载&#39;到&#39;重载()&#39;在任务中
在线cout << "Overloaded object value: " << ObjectThree.number << endl;
- 请求会员编号&#39;在&#39; ObjectThree&#39;中,它是非类类型&#39;重载()&#39;。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

Overloading ObjectThree();函数原型

声明变量ObjectThree。所以你的编译器会输出一个错误,虽然这个错误令人困惑。

这是一个非常常见的错误:Google为#34;最令人烦恼的解析&#34;。