operator<< overload question

O

Ook

This is my code in it's entireity:


#include <iostream>
using namespace std;
template <typename T>
class SortedList
{
public:
friend ostream& operator<< (ostream& os, const SortedList<T>& lst );
};
template <typename T>
ostream& operator<< (ostream& os, const SortedList<T>& lst)
{
return os;
}
int main()
{
SortedList<int> sList;
//This compiles fine until I add cout << sList; in main():
cout << sList;
return 0;
}

If I comment out cout <, sList;, it compiles fine. When I add cout << sList,
I get compile error.

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
SortedList<int> const &)"
(??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$SortedList@H@@@Z)
referenced in function _main

I know this is a specific compiler error and this is not a specific compiler
newsgroup, but I'm hoping someone can tell from the above what I'm doing
wrong.
 
A

Andrej Hristoliubov

Ook said:
This is my code in it's entireity:


#include <iostream>
using namespace std;
template <typename T>
class SortedList
{
public:
friend ostream& operator<< (ostream& os, const SortedList<T>& lst );
};
template <typename T>
ostream& operator<< (ostream& os, const SortedList<T>& lst)
{
return os;
}
int main()
{
SortedList<int> sList;
//This compiles fine until I add cout << sList; in main():
cout << sList;
return 0;
}

If I comment out cout <, sList;, it compiles fine. When I add cout << sList,
I get compile error.

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
SortedList<int> const &)"
(??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$SortedList@H@@@Z)
referenced in function _main

I know this is a specific compiler error and this is not a specific compiler
newsgroup, but I'm hoping someone can tell from the above what I'm doing
wrong.

The problem here is that your template function cannot deduce
SortedList<int> from paramter passed in.
 
O

Ook

Andrej Hristoliubov said:
The problem here is that your template function cannot deduce
SortedList<int> from paramter passed in.

If I replace:
template <typename T>
ostream& operator<< (ostream& os, const SortedList<T>& lst)

with

ostream& operator<< (ostream& os, const SortedList<int>& lst)

It compiles, but now it's not at a template function. How, then, do I get my
template function to accept any type, instead of specifying the type? That
is why I had it as a templayte function with SortedList<T>, so that I would
not have to specify the type. Or do I have to write a different function for
each type I'm going to use, ie:

ostream& operator<< (ostream& os, const SortedList<int>& lst)
ostream& operator<< (ostream& os, const SortedList<char>& lst)

etc. ?
 
B

Branimir Maksimovic

"Ook" <Ook Don't send me any freakin' spam at zootal dot com delete the
Don't send me any freakin' spam> wrote in message
This is my code in it's entireity:


#include <iostream>
using namespace std;
template <typename T>
class SortedList
{
public:
friend ostream& operator<< (ostream& os, const SortedList<T>& lst );
};
template <typename T>
ostream& operator<< (ostream& os, const SortedList<T>& lst)
{
return os;
}

This is in faq:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

Greetings, Bane.
 
A

Andrej Hristoliubov

Ook said:
If I replace:
template <typename T>
ostream& operator<< (ostream& os, const SortedList<T>& lst)

with

ostream& operator<< (ostream& os, const SortedList<int>& lst)

It compiles, but now it's not at a template function. How, then, do I get my
template function to accept any type, instead of specifying the type? That
is why I had it as a templayte function with SortedList<T>, so that I would
not have to specify the type. Or do I have to write a different function for
each type I'm going to use, ie:

ostream& operator<< (ostream& os, const SortedList<int>& lst)
ostream& operator<< (ostream& os, const SortedList<char>& lst)

etc. ?


follow the link in my previous post; it addresses your problem quite
well. The one I've posted was a simplified solution.

Also, you need to sharpen your template knowledge. I suggest reading
C++ teplates.
 
O

Ook

Andrej Hristoliubov said:
1.
#include <iostream>

using std::eek:stream;
using std::cout;

template <typename T>
class SortedList
{
public:
friend ostream& operator<< (ostream& os, const SortedList<T>& lst)
{
return os;

}

}

int main()
{
SortedList<int> sList;
cout << sList;
return 0;



}

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

Ahh...put implementation in class, ok, read that in the faq. I also found
that declaring it like this:

template <typename T>
friend ostream& operator<< (ostream& os, const SortedList<T>& lst );

Also works - but I already prefaced my class decleration with template
<typename T> so I'm not sure it's right to do it again in the class. I need
to read faq again...tomorrow...when I have more braincells...
 
A

Andrej Hristoliubov

Branimir said:
"Ook" <Ook Don't send me any freakin' spam at zootal dot com delete the
Don't send me any freakin' spam> wrote in message


This is in faq:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

Greetings, Bane.


Bane-

Before you respond, at least, give a perfunctory skim of above
messages. This link I have already posted and no need to reiterate. We
Russians, do NOT tolerate this kind of behavior and will punish with
all might.
 
B

Branimir Maksimovic

Andrej Hristoliubov said:
Bane-

Before you respond, at least, give a perfunctory skim of above
messages. This link I have already posted and no need to reiterate. We
Russians, do NOT tolerate this kind of behavior and will punish with
all might.

Hey, transfer of news messages is not in real time :)
Do you think that I would bother to send this if I saw that you posted that
link :)


Greetings, Bane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top