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

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_ostream<char,struct std::char_traits<char> > & __cdecl
operator<<(class std::basic_ostream<char,struct std::char_traits<char> >
&,class Test<int> &)"
(??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ 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 8418

"Yoon-Soo Lee" <yo****@vt.edu> wrote in message news:bt**********@solaris.cc.vt.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_ostream<char,struct std::char_traits<char> > & __cdecl
| operator<<(class std::basic_ostream<char,struct std::char_traits<char> >
| &,class Test<int> &)"
| (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ 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******@bigpond.com.au> wrote in message
news:bt************@ID-110726.news.uni-berlin.de...

"Yoon-Soo Lee" <yo****@vt.edu> wrote in message news:bt**********@solaris.cc.vt.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_ostream<char,struct std::char_traits<char> > & __cdecl
| operator<<(class std::basic_ostream<char,struct std::char_traits<char> >
| &,class Test<int> &)"
| (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ 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.edu> wrote in message news:bt**********@solaris.cc.vt.edu...
|
| "Chris ( Val )" <ch******@bigpond.com.au> wrote in message
| news:bt************@ID-110726.news.uni-berlin.de...
| >
| > "Yoon-Soo Lee" <yo****@vt.edu> wrote in message
| news:bt**********@solaris.cc.vt.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_ostream<char,struct std::char_traits<char> > & __cdecl
| > | operator<<(class std::basic_ostream<char,struct std::char_traits<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******@bigpond.com.au> wrote in message
news:bt************@ID-110726.news.uni-berlin.de...

"Yoon-Soo Lee" <yo****@vt.edu> wrote in message news:bt**********@solaris.cc.vt.edu... |
| "Chris ( Val )" <ch******@bigpond.com.au> wrote in message
| news:bt************@ID-110726.news.uni-berlin.de...
| >
| > "Yoon-Soo Lee" <yo****@vt.edu> wrote in message
| news:bt**********@solaris.cc.vt.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_ostream<char,struct std::char_traits<char> > & __cdecl
| > | operator<<(class std::basic_ostream<char,struct std::char_traits<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***@blueyonder.co.uk> wrote in message
news:BK*****************@news-binary.blueyonder.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
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...
15
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...
1
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);...
3
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...
1
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...
1
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...
4
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...
0
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...
0
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...
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
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
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...

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.