473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Template problems in Visual C++ 6

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?

Jul 23 '05 #1
6 2271
Starx wrote:
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.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #2
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?

Jul 23 '05 #3
Starx wrote:
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+()?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #4
Starx wrote:
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
Jul 23 '05 #5

"Starx" <js****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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 <class numericType> testClass<numericType> operator +(testClass<numericType> a, const numericType c) {
testClass<numericType> returnVal;
returnVal.x = a.x + c;
return returnVal;
}


Bill Shortall
Jul 23 '05 #6
Starx wrote:
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);

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gonçalo Rodrigues | last post by:
Hi all, Sorry to bother you guys again with a template question but I'm really trying to understand them and there's some piece of info that I'm missing. I'm sure I'm being really dumb here so,...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
13
by: Thomas J. Clancy | last post by:
I was just wondering. After all these years of working on Visual C++, why hasn't Microsoft yet seen fit to fully implement the template portion of the C++ standard when so many other vendors,...
2
by: Itjalve | last post by:
This gives me a fatal error. I'm using .NET VC7.1 and made a win32 consol app, I have no problems with VC6. Debug build. I have removed nearly all my code this is whats left. From the beginning...
4
by: Chuck Chopp | last post by:
I have an application that I originally built using Visual Studio .NET 2003 as native C++ . This application includes a template class that was derived from the string class that's part of the C++...
11
by: hogtiedtoawaterbuffalo | last post by:
I have a template class that works fine when I implement it with <int>, but when I use <floator <doubleit doesn't work. The class has a dynamic array of type T that gets instantiated in my...
8
by: vectorizor | last post by:
Hi all, I have a slight problem with the usage of function templates. It shoud be really easy for you guys to explain me what's wrong. I have one base class and 2 derived classes: /*code*/...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.