Reputation: 27
The code doesn't link when I try to compile, the error is "undefined reference to operator*" and I think this may be the problem.
This is my .h file:
namespace space{
class Number{
.
.
public:
friend Number operator*(double scaler, const Number& one);
private:
int val;
std::string unit;
.
}
This is the .cpp file:
using namespace space;
Number operator*(double scaler, const Number& one){
Number temp((one.val * scaler), one.unit);
return temp;
}
But I'm getting the error (it's right on the one.val
):
member "space::Number::val" (declared at line 47 of ".../Number.hpp") is inaccessibleC/C++(265)
Upvotes: 0
Views: 199
Reputation: 122830
The friend declaration is a declaration of a function:
namespace space{
class Number{
friend Number operator*(double scaler, const Number& one);
// ...
};
As this is inside a namespace, the function that is declared is space::operator*
. Then you define ::operator*
in the global namespace. Those are two different functions. You made one operator a friend of the class, but define a different one.
The operator that you made a friend must be defined inside the namespace (or you declare the one in global scope as friend):
namespace space {
Number operator*(double scaler, const Number& one){
Number temp((one.val * scaler), one.unit);
return temp;
}
}
Upvotes: 1