473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template function problem

Dear all,

I am trying to design classes with stream support. Basically, I want
the operator << work for the base class and all the derived classes.

If the base class is a template class, and the operator << is also
designed as a template function. The program has following linking
errors:

"error LNK2019: unresolved external symbol "class
std::basic_ostr eam<char,struct std::char_trait s<char & __cdecl
operator<<(clas s std::basic_ostr eam<char,struct std::char_trait s<char>
&,class Base<boolconst &)" (??6@YAAAV?$bas ic_ostream@DU?
$char_traits@D@ std@@@std@@AAV0 1@ABV?$Base@_N@ @@Z) referenced in
function _main"

However, if the base class is not a template class. Everything is ok
and the link error dosen't exist. Note that I tried instantiate the
operator << explicitly. But it doesn't solve the problem.

Here are the example codes:
----------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>

using namespace std;

template<class T>
class Base
{
public:
friend ostream& operator << (ostream&, const Base&);
virtual string get_buffer() const {return "Base class";}
};

class Derived : public Base<bool>
{
public:
virtual string get_buffer() const {return "Derived class";}
};

template<class T>
ostream& operator << (ostream& os, const Base<T>& a)
{
os << a.get_buffer();
return os;
}

/*
template<>
ostream& operator << (ostream& os, const Base<bool>& a)
*/

int main()
{
Derived b;
cout << b << endl;
}

Jun 22 '07 #1
2 1964
syang8 wrote:
I am trying to design classes with stream support. Basically, I want
the operator << work for the base class and all the derived classes.

If the base class is a template class, and the operator << is also
designed as a template function. The program has following linking
errors:

"error LNK2019: unresolved external symbol "class
std::basic_ostr eam<char,struct std::char_trait s<char & __cdecl
operator<<(clas s std::basic_ostr eam<char,struct std::char_trait s<char>
>&,class Base<boolconst &)" (??6@YAAAV?$bas ic_ostream@DU?
$char_traits@D@ std@@@std@@AAV0 1@ABV?$Base@_N@ @@Z) referenced in
function _main"
The friend declaration in your code instroduces a non-template function
into the namespace, which the compiler picks (and wants to call), and
that's what the linker is missing. If you replace it with a declaration
of a _template_, it will have a need to instantiate your op<< template.

Make sure that when declaring a function for the first time in a friend
declaration, you don't declare it wrongly.
>
However, if the base class is not a template class. Everything is ok
and the link error dosen't exist. Note that I tried instantiate the
operator << explicitly. But it doesn't solve the problem.

Here are the example codes:
----------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>

using namespace std;
Add here:

template<class Tclass Base;
template<class Tostream& operator << (ostream&, const Base<T>&);
>
template<class T>
class Base
{
public:
friend ostream& operator << (ostream&, const Base&);
Replace with

friend ostream& operator << <T(ostream&, const Base&);
virtual string get_buffer() const {return "Base class";}
};

class Derived : public Base<bool>
{
public:
virtual string get_buffer() const {return "Derived class";}
};

template<class T>
ostream& operator << (ostream& os, const Base<T>& a)
{
os << a.get_buffer();
return os;
}

/*
template<>
ostream& operator << (ostream& os, const Base<bool>& a)
*/

int main()
{
Derived b;
cout << b << endl;
}
It should now compile.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #2
Yes. My bad. messed up simple things.

On Jun 22, 4:58 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
syang8 wrote:
I am trying to design classes with stream support. Basically, I want
the operator << work for the base class and all the derived classes.
If the base class is a template class, and the operator << is also
designed as a template function. The program has following linking
errors:
"error LNK2019: unresolved external symbol "class
std::basic_ostr eam<char,struct std::char_trait s<char & __cdecl
operator<<(clas s std::basic_ostr eam<char,struct std::char_trait s<char>
&,class Base<boolconst &)" (??6@YAAAV?$bas ic_ostream@DU?
$char_traits@D@ std@@@std@@AAV0 1@ABV?$Base@_N@ @@Z) referenced in
function _main"

The friend declaration in your code instroduces a non-template function
into the namespace, which the compiler picks (and wants to call), and
that's what the linker is missing. If you replace it with a declaration
of a _template_, it will have a need to instantiate your op<< template.

Make sure that when declaring a function for the first time in a friend
declaration, you don't declare it wrongly.
However, if the base class is not a template class. Everything is ok
and the link error dosen't exist. Note that I tried instantiate the
operator << explicitly. But it doesn't solve the problem.
Here are the example codes:
---------------------------------------------------------------------------*-------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;

Add here:

template<class Tclass Base;
template<class Tostream& operator << (ostream&, const Base<T>&);
template<class T>
class Base
{
public:
friend ostream& operator << (ostream&, const Base&);

Replace with

friend ostream& operator << <T(ostream&, const Base&);


virtual string get_buffer() const {return "Base class";}
};
class Derived : public Base<bool>
{
public:
virtual string get_buffer() const {return "Derived class";}
};
template<class T>
ostream& operator << (ostream& os, const Base<T>& a)
{
os << a.get_buffer();
return os;
}
/*
template<>
ostream& operator << (ostream& os, const Base<bool>& a)
*/
int main()
{
Derived b;
cout << b << endl;
}

It should now compile.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

Jun 22 '07 #3

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

Similar topics

4
2599
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in the code below. The function template < typename T >
0
1628
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the...
4
2334
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a .h and a .cpp file, gcc throws some strange linking errors. Compiling like this:
7
2120
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
3926
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. template<class Type> class base { public: base& operator/=(const base&); Type *image;
7
354
by: hopangtsun | last post by:
Hi all, I have encountered a problem on using the function template my goal is to add two numbers, which they can be int or double if I do this in this way template<class T> T addition(T a, T b){ return (a + b); } it can only deal with int + int or double + double however, I also want int + double, double + int so I come up with...
19
7898
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
3
3743
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out....
5
1732
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; // function object
3
2906
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7778
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
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 we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.