473,503 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template confusion

Hello,

Consider the following short code snippet:

template <class TType1> class CA
{
public:
friend ostream& operator<<( ostream &o, CA<TType1> a )
{
// do something reasonable here...
return o;
}
};

template <class TType1> class CB
{
public:
CB()
{
CA<int> Data;
cout << Data << endl;
}
};

int main( int argc, char *argv[] )
{
CB<int> a;
return 1;
}

Can someone explain why MSVC 6.0 informs me that:

error C2679: binary '<<' : no operator defined which takes a right-hand
operand of type 'class CA<int>' (or there is no acceptable conversion)

It appears that the construction of the template member functions isn't
occuring as I would expect. I admit I'm not a compiler expert. What's the
skinny here and what's the best solution? I've found a few solutions but
they all seem convoluted - basically I instance a CA<int> in various other
places or I have CB inherit from CA.

Thanks,
Mike
Jul 22 '05 #1
1 1313
"Michael W. Hicks" <mh*****@speakeasy.net> wrote...
Consider the following short code snippet:

template <class TType1> class CA
{
public:
friend ostream& operator<<( ostream &o, CA<TType1> a )
{
// do something reasonable here...
return o;
}
};

template <class TType1> class CB
{
public:
CB()
{
CA<int> Data;
cout << Data << endl;
}
};

int main( int argc, char *argv[] )
{
CB<int> a;
return 1;
}

Can someone explain why MSVC 6.0 informs me that:

error C2679: binary '<<' : no operator defined which takes a right-hand
operand of type 'class CA<int>' (or there is no acceptable conversion)

It appears that the construction of the template member functions isn't
occuring as I would expect. I admit I'm not a compiler expert. What's the
skinny here and what's the best solution? I've found a few solutions but
they all seem convoluted - basically I instance a CA<int> in various other
places or I have CB inherit from CA.


You should probably declare the function before trying to define it.
Declaring it (and you want it a template) requires to put 'template'
there. That would give your compiler a notion that the function is
actually a template.

template<class T> class CA;
template<class T> ostream& operator<<(ostream&, CA<T> const&);

....

Victor
Jul 22 '05 #2

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

Similar topics

2
6258
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
4
2730
by: Grey Plastic | last post by:
I have several classes that all keep track of static data. However, the manner that they keep track of static data is identical, and so I'm using the template<class Child> class Parent { ... };...
31
3431
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
2
1186
by: JT | last post by:
I have a compiler error when using a non-dependent type declared in a template, if I use the type in function definitions. I think it is a parsing issues related to confusion with a typename. ...
8
11343
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
11
1850
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
16
2146
by: christopher diggins | last post by:
It appears that the following is not considered a class: template<typename T> class C { }; ? So officially is this considered: a class, a template, a class template, or a template class? I...
3
1725
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
14
1791
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
4
1383
by: Jason | last post by:
/* I have never use template before, so bear with me. Here is what I am trying to do: I declare an abstract base class MatrixInterface with template to define an interface for all my...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7258
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
7313
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...
1
6970
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
7441
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...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.