Reputation:
I believe my problem is very fundamental, but I am not finding a solution to it. I have tried several workarounds. The errors I'm getting are a very long list of "no matching function call to's". I am pretty sure it has to do with how I'm working with namespaces, but looking online it seems like I am formatting it correctly. The namespace thing is a requirement for an assignment so I cannot remove it.
Code:
namespace complexSpc{
class complex
{
public:
friend complex operator*(complex &, complex &);
};
}
At the bottom of my program, I define my class members. They are not within a namespace block. I tried to do that but it didn't fix anything. I also tried to move the definitions so they are defined when they are declared.
complexSpc::complex complexSpc :: operator*(complexSpc::complex & first, complexSpc::complex & second)
{
complexSpc::complex t1 = complexSpc::complex(first.real() * second.real(), first.imag() * second.imag());
return t1;
}
The error produced for this is like so:
complexFix.cpp:183: error: no matching function for call to ‘complexSpc::complex ::complex(complexSpc::complex)’
Someone has asked if I have a copy constructor. I do. It is declared in the namespace and defined like so:
complex(complex &);
complexSpc::complex::complex(complexSpc::complex & copy)
{
member1 = copy.real();
member2 = copy.imag();
return;
}
Upvotes: 0
Views: 951
Reputation: 88225
complexFix.cpp:183: error: no matching function for call to ‘complexSpc::complex ::complex(complexSpc::complex)’
Your original code includes the line:
complexSpc::complex t1 = complexSpc::complex(member1 + second.real(), member2 + second.imag());
Which constructs a temporary and then copy-constructs t1
with it. However your copy constsructor is declared:
complex(complex &);
Which means it cannot take temporaries as arguments, because non-const (lvalue) references are not permitted to bind to temporary objects. Note: Visual Studio has an extension which does allow temporaries to bind to non-const lvalue references, so it won't catch this error.
const references are allowed to bind to temporaries, so if you change the copy constructor to:
complex(complex const &);
Then the compiler will be able to find the matching function it's looking for in that error message.
Also note that there are many other functions in your code with non-const reference parameters. Those may not be causing errors yet as you're not using those functions with temporaries, but they will have to be updated as well in order to work with temporaries.
complexNumbers.cpp:182: error: ‘complexSpc::complex complexSpc::operator*(complexSpc::complex&, complexSpc::complex&)’ should have been declared inside ‘complexSpc’
I believe this is actually a bug in the compiler. The friend declarations inside the class should be sufficient, however gcc doesn't appear to properly handle that in all cases. A workaround is to declare those friend functions it's erroring on a second time, but outside the class:
namespace N {
class C {
friend void f();
};
void f(); // workaround.
}
void N::f() {}
Upvotes: 1
Reputation: 1
Change the signature of your copy constructor to:
complexSpc::complex::complex(const complexSpc::complex & copy);
To allow rvalues of complex being copied on the return statement of your operator definition.
Note that you'll have to stick to this principle for your operator signatures also, to be able to use them in combined operations:
complexSpc::complex complexSpc :: operator*
( const complexSpc::complex & first
, const complexSpc::complex & second
);
Most probably this should fix most of your remaining errors.
Upvotes: 1