Q about operator overloading and default constructors

Could someone please tell me why the code below doesn't compile?

I'm specifically interested to know why the call at line 22 to initialize with a default constructor breaks it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class MyClass
{
private:
	int m_x;
	public:
	MyClass(int init) { m_x = init; }
	MyClass()
	{
		m_x = 7;
	}
	friend MyClass operator+ (MyClass &Class1, MyClass &Class2);
	int getm() { return m_x; }
};

MyClass operator+ (MyClass &Class1, MyClass &Class2)
{
	return MyClass (Class1.m_x + Class2.m_x);
}

int main()
{
	MyClass ClassA();
	MyClass ClassB(101);
	MyClass AddClass(ClassA + ClassB);
	std::cout << AddClass.getm() << "\n";
}
Last edited on
Line 22:ClassA is interpreted as a function that returns MyClass. Just remove the () after ClassA.
Oops, silly mistake, thanks!
Topic archived. No new replies allowed.