Template problems in Visual C++ 6

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?
 
S

Steven T. Hatton

Starx said:
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:

Try adding a conversion operator to the type you are working with. Templates
are pretty stubborn about type conversion.
 
S

Starx

But because the type is templated I can't add a conversion operator. I
don't know what type I'll be working with.

Wouldn't the testClass(numericType) constructor handle it anyway?
 
S

Steven T. Hatton

Starx said:
But because the type is templated I can't add a conversion operator. I
don't know what type I'll be working with.

Wouldn't the testClass(numericType) constructor handle it anyway?
Have you tried creating a member fuction template operator+=(), and then
using that to create the namespace local binary operator+()?
 
K

Kai-Uwe Bux

Starx said:
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?

To me, templates and friends are non-intuitive. Try declaring things
beforehand:

template < class T >
class testClass;

template < class T >
testClass<T> operator+ ( testClass<T>, testClass<T> );


Now define:

template <class numericType>
class testClass
{
public:
testClass(numericType val = 0);

// Note the <>:
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;
}



Best

Kai-Uwe Bux
 
B

Bill Shortall

Starx said:
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?

I think you will need to define an operator for addition by a constant e.g
something like

template said:
testClass<numericType> operator +(testClass<numericType> a, const numericType c)
{
testClass<numericType> returnVal;
returnVal.x = a.x + c;
return returnVal;
}

Bill Shortall
 
J

John Shell

Starx said:
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?
Add this function:

template <class numericType>
testClass<numericType> operator +(testClass<numericType> a,
numericType b)
{
testClass<numericType> returnVal;
returnVal.x = a.x + b;
return returnVal;
}

Or provide an explicit conversion, like this:
a = b + testClass<int>(2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top