Sunday, 22 May 2016

c++ - Template operator- creates error

I'm working with a BigInt class which I've derived from another InfInt class, but this class only has an operator-() for its own type. In order to avoid having to do this



BigInt one=1;

BigInt foo,bar;
bar=foo-one;


just to be able to do this,



bar=foo-1;


I've overloaded the operator- function with a template version of it:




template
BigInt BigInt::operator-(const T& rhs)
{
return (*this-(BigInt)rhs);
}


But when I try to compile I get this error:




Undefined reference to 'BigInt BigInt::operator-(int const&)'


I'm one hundred percent convinced it's a stupid beginner mistake, but I've spent the last 45 minutes trying to find a solution. Also, I'm sorry if this question has already been answered, but I've tried searching both on google and using stackoverflow's internal search engine and I haven't found anything.



Edit: Actually was a duplicate of this, moving the implementation to the header fixed it for me.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...