S
Starx
With the following class:
template <class numericType>
class testClass
{
public:
testClass(numericType val = 0);
friend testClass operator +(testClass a, testClass b);
private:
numericType x;
};
template <class numericType>
testClass<numericType>::testClass(numericType val)
: x(val)
{
}
template <class numericType>
testClass<numericType> operator +(testClass<numericType> a,
testClass<numericType> b)
{
testClass<numericType> returnVal;
returnVal.x = a.x + b.x;
return returnVal;
}
When I use the overloaded addition operator on two testClass objects (a
+ b) things work fine. But when I try to add a testClass object with
another type, for example:
testClass<int> a, b = 2;
a = b + 2;
I get these two errors:
error C2784: 'class testClass<numericType> __cdecl operator +(class
testClass<numericType>,class testClass<numericType>)' : could not
deduce template argument for 'class testClass<numericType>' from 'class
testClass<int>'
error C2676: binary '+' : 'class testClass<int>' does not define this
operator or a conversion to a type acceptable to the predefined
operator
Can anyone tell me what's causing these errors and how I could get rid
of them?
template <class numericType>
class testClass
{
public:
testClass(numericType val = 0);
friend testClass operator +(testClass a, testClass b);
private:
numericType x;
};
template <class numericType>
testClass<numericType>::testClass(numericType val)
: x(val)
{
}
template <class numericType>
testClass<numericType> operator +(testClass<numericType> a,
testClass<numericType> b)
{
testClass<numericType> returnVal;
returnVal.x = a.x + b.x;
return returnVal;
}
When I use the overloaded addition operator on two testClass objects (a
+ b) things work fine. But when I try to add a testClass object with
another type, for example:
testClass<int> a, b = 2;
a = b + 2;
I get these two errors:
error C2784: 'class testClass<numericType> __cdecl operator +(class
testClass<numericType>,class testClass<numericType>)' : could not
deduce template argument for 'class testClass<numericType>' from 'class
testClass<int>'
error C2676: binary '+' : 'class testClass<int>' does not define this
operator or a conversion to a type acceptable to the predefined
operator
Can anyone tell me what's causing these errors and how I could get rid
of them?