473,399 Members | 3,603 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,399 software developers and data experts.

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 1312
"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
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
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
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
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
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
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
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
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
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
4
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.