473,549 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking Error unresolved external symbo when using template and friend

I am using Visual C++ .NET 2003 and running into some linking error from the
following template code.
The error messages is
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 Test<int> &)"
(??6@YAAAV?$bas ic_ostream@DU?$ char_traits@D@s td@@@std@@AAV01 @AAV?$Test@H@@@ Z
) referenced in function _main
#include <iostream>
using namespace std;

template <class object>
class Test {
private:
object T;
public:
Test(object x): T(x) {};
friend ostream & operator << (ostream & out_stream, Test &test);

};

template <class object>
ostream & operator << (ostream & out_stream, Test<object> &test) {
out_stream << test.T ;
return out_stream;
}

cannot figure out what the problem is. Am I writing my template wrong or
what?
Jul 22 '05 #1
5 8434

"Yoon-Soo Lee" <yo****@vt.ed u> wrote in message news:bt******** **@solaris.cc.v t.edu...
| I am using Visual C++ .NET 2003 and running into some linking error from the
| following template code.
| The error messages is
| 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 Test<int> &)"
| (??6@YAAAV?$bas ic_ostream@DU?$ char_traits@D@s td@@@std@@AAV01 @AAV?$Test@H@@@ Z
| ) referenced in function _main

[snip]

| cannot figure out what the problem is. Am I writing my template wrong or
| what?

Try this:

// Forward declarations...
template <class object>
class Test;

template <class object>
ostream & operator << (ostream & out_stream, Test<object> &test);

template <class object>
class Test {
private:
object T;
public:
Test(object x): T(x) {};
friend ostream & operator << <> (ostream & out_stream, Test<object> &test);

};

template <class object>
ostream & operator << (ostream & out_stream, Test<object> &test) {
out_stream << test.T ;
return out_stream;
}

Take note of the change to the friend prototype.

Cheers.
Chris Val
Jul 22 '05 #2

"Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
news:bt******** ****@ID-110726.news.uni-berlin.de...

"Yoon-Soo Lee" <yo****@vt.ed u> wrote in message news:bt******** **@solaris.cc.v t.edu... | I am using Visual C++ .NET 2003 and running into some linking error from the | following template code.
| The error messages is
| 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 Test<int> &)"
| (??6@YAAAV?$bas ic_ostream@DU?$ char_traits@D@s td@@@std@@AAV01 @AAV?$Test@H@@@ Z | ) referenced in function _main

[snip]

| cannot figure out what the problem is. Am I writing my template wrong or
| what?

Try this:

// Forward declarations...
template <class object>
class Test;

template <class object>
ostream & operator << (ostream & out_stream, Test<object> &test);

template <class object>
class Test {
private:
object T;
public:
Test(object x): T(x) {};
friend ostream & operator << <> (ostream & out_stream, Test<object> &test);
};

template <class object>
ostream & operator << (ostream & out_stream, Test<object> &test) {
out_stream << test.T ;
return out_stream;
}

Take note of the change to the friend prototype.

Cheers.
Chris Val


I figured out that forward declaration was unnecessary in my fragment of
code. Is it just a good programming habit? Also I would like to hear some
brief explanation why the prototype has to change in the way you have told
me to. Thank you.
Jul 22 '05 #3

"Yoon-Soo Lee" <yo****@vt.ed u> wrote in message news:bt******** **@solaris.cc.v t.edu...
|
| "Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
| news:bt******** ****@ID-110726.news.uni-berlin.de...
| >
| > "Yoon-Soo Lee" <yo****@vt.ed u> wrote in message
| news:bt******** **@solaris.cc.v t.edu...
| > | I am using Visual C++ .NET 2003 and running into some linking error from
| the
| > | following template code.
| > | The error messages is
| > | 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 Test<int> &)"

[snip]

| I figured out that forward declaration was unnecessary in my fragment of
| code. Is it just a good programming habit?

Well, your right(you might not need the forward declarations), it is
just a workaround that I use for BCB5.

| Also I would like to hear some brief explanation why the prototype
| has to change in the way you have told me to. Thank you.

Ok, in your original friend declaration:
friend ostream & operator << (ostream & out_stream, Test &test);

....the ostream operator '<<' is declared as a 'NON-TEMPLATE' function.

Then you implement a 'template' version of operator '<<', expecting
it to be the implementation for the 'non-template' version:

template <class object>
ostream & operator << (ostream & out_stream, Test &test) {
out_stream << test.T ;
return out_stream;
}

The brackets '<>' in the friend declaration, tell the compiler
to use/instantiate the templated version of operator '<<', rather
than a new 'non-templated' one.

friend ostream & operator << <> (ostream & out_stream, Test<object> &test);

Where you could also do this:

friend ostream & operator << <object> (ostream & out_stream, Test<object> &test);
Now they are templated declarations - what you really wanted.

Cheers.
Chris Val
Jul 22 '05 #4

"Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
news:bt******** ****@ID-110726.news.uni-berlin.de...

"Yoon-Soo Lee" <yo****@vt.ed u> wrote in message news:bt******** **@solaris.cc.v t.edu... |
| "Chris ( Val )" <ch******@bigpo nd.com.au> wrote in message
| news:bt******** ****@ID-110726.news.uni-berlin.de...
| >
| > "Yoon-Soo Lee" <yo****@vt.ed u> wrote in message
| news:bt******** **@solaris.cc.v t.edu...
| > | I am using Visual C++ .NET 2003 and running into some linking error from | the
| > | following template code.
| > | The error messages is
| > | 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 Test<int> &)"

[snip]

| I figured out that forward declaration was unnecessary in my fragment of
| code. Is it just a good programming habit?

Well, your right(you might not need the forward declarations), it is
just a workaround that I use for BCB5.

| Also I would like to hear some brief explanation why the prototype
| has to change in the way you have told me to. Thank you.

Ok, in your original friend declaration:
friend ostream & operator << (ostream & out_stream, Test &test);

...the ostream operator '<<' is declared as a 'NON-TEMPLATE' function.

Then you implement a 'template' version of operator '<<', expecting
it to be the implementation for the 'non-template' version:

template <class object>
ostream & operator << (ostream & out_stream, Test &test) {
out_stream << test.T ;
return out_stream;
}

The brackets '<>' in the friend declaration, tell the compiler
to use/instantiate the templated version of operator '<<', rather
than a new 'non-templated' one.

friend ostream & operator << <> (ostream & out_stream, Test<object> &test);
Where you could also do this:

friend ostream & operator << <object> (ostream & out_stream, Test<object> &test);

Now they are templated declarations - what you really wanted.

Cheers.
Chris Val


A simpler approach for those of us who are antisocial and dislike friends is
simply to declare a member
ostream& print(ostream&) const;
And then define a non-friend template in terms of print.
Jul 22 '05 #5

"Nick Hounsome" <nh***@blueyond er.co.uk> wrote in message
news:BK******** *********@news-binary.blueyond er.co.uk...

[snip]

| A simpler approach for those of us who are antisocial and dislike friends is
| simply to declare a member
| ostream& print(ostream&) const;
| And then define a non-friend template in terms of print.

Yes, that is an alternative, and often used in an inheritance
hierarchy with polymorphism, but my aim was to provide the OP
with a fix or solution to the problem code presented :-).

Cheers.
Chris Val
Jul 22 '05 #6

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

Similar topics

0
1255
by: tertius | last post by:
Hi All, I embedded some Python/C API functions in my C dll. I use the Borland freecommandline tools. I converted the python23.lib with coff2omf but still get unresolved externals. If I use coff2omf like this ... (default)
15
1729
by: Improving | last post by:
I have a template class that has static members, so in the .cpp file I have defined them with templated definitions. Now when in a different ..cpp file that includes the header file for the template class I derive a new class from the template class and pass that class as its template parameter (CRTP). Now when I instantiate this new class it...
1
3292
by: R | last post by:
hello. I've got problem with linking asm and c procedures. I've got main.c where i've got my external definitions: extern int buffer(int,int,int); // coded in asm extern int hash(int,int); //coded in asm I've got simple make file for my project:
3
5844
by: YAN KANG / SU | last post by:
Hi, All I am migrating to Studio .NET 2003 from Studio 6.0 and Studio .NET 2002. When I compiled my code, which is compilable both in VC++ 6.0 and Studio .NET 2002, I have an error LNK2019 as the following ------------------------------------------------------------------------- error LNK2019: unresolved external symbol "class CPV3<double>...
1
1912
by: endo55 | last post by:
Hi I've got the following errors when trying to compile a program cvision error LNK2001: unresolved external symbol _IID_IGraphBuilde cvision error LNK2001: unresolved external symbo _IID_IFileSinkFilter cvision error LNK2001: unresolved external symbol _IID_ICreateDevEnu cvision error LNK2001: unresolved external symbol _IID_IBaseFilte...
1
8107
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip My problem is that when I try to compile the library I got a list of linking error messages. I am not a specialist of porting C++ code to .Net....
4
6396
by: Sanjay Kumar | last post by:
Folks ! I am working with VC++ after a long time and having problem linking latest xerces 2.7 in VC++ 2005 Express Edition. I have done following: 1. downloaded and unpacked the the library: http://www.apache.org/dist/xml/xerces-c/binaries/xerces-c_2_7_0-windows_2000-msvc_60.zip
0
4876
by: Adam Clauss | last post by:
I have managed C++ library (is bridging between a Win32 .dll and a C# application). All was well when compiled under VS2003, but I am running into a series of linking errors when compiling against VS2005. They all/mostly seem to be within the STL. Any idea what might cause something like this? Linking errors follow. ---
0
2546
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when using the /MT compilation option. Our debug solution's /MTd flag works fine and using /MD also seems to work ok. For some reason I can't fathom,...
0
7541
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7979
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7826
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
6065
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...
1
5385
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5107
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
3512
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...
1
1960
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
0
781
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.