473,386 Members | 1,969 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,386 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 8415

"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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
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...

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.