473,396 Members | 1,779 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,396 software developers and data experts.

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_ostream<char,struct std::char_traits<char & __cdecl
operator<<(class std::basic_ostream<char,struct std::char_traits<char>
&,class Base<boolconst &)" (??6@YAAAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@AAV01@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 1954
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_ostream<char,struct std::char_traits<char & __cdecl
operator<<(class std::basic_ostream<char,struct std::char_traits<char>
>&,class Base<boolconst &)" (??6@YAAAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@AAV01@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...@comAcast.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_ostream<char,struct std::char_traits<char & __cdecl
operator<<(class std::basic_ostream<char,struct std::char_traits<char>
&,class Base<boolconst &)" (??6@YAAAV?$basic_ostream@DU?
$char_traits@D@std@@@std@@AAV01@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
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...
0
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...
4
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...
7
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
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. ...
7
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...
19
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...
3
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...
5
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; //...
3
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
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...
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,...

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.