By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,512 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,512 IT Pros & Developers. It's quick & easy.

operator<< overload question

P: n/a
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.
Oct 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

Ook wrote:
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.

Oct 23 '05 #2

P: n/a

Andrej Hristoliubov wrote:
Ook wrote:
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.


1.
#include <iostream>

using std::ostream;
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-lit...html#faq-35.16

Oct 23 '05 #3

P: n/a
Ook

"Andrej Hristoliubov" <cp************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Ook wrote:
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.


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. ?
Oct 23 '05 #4

P: n/a

Ook wrote:
"Andrej Hristoliubov" <cp************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Ook wrote:
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.


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.

Oct 23 '05 #5

P: n/a
Ook

"Andrej Hristoliubov" <cp************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Andrej Hristoliubov wrote:
Ook wrote:
> 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.


1.
#include <iostream>

using std::ostream;
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-lit...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...
Oct 23 '05 #6

P: n/a

"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
news:eO********************@giganews.com...
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-lit...html#faq-35.16

Greetings, Bane.
Oct 23 '05 #7

P: n/a

Branimir Maksimovic wrote:
"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
news:eO********************@giganews.com...
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-lit...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.

Oct 23 '05 #8

P: n/a

"Andrej Hristoliubov" <cp************@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Branimir Maksimovic wrote:
"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
news:eO********************@giganews.com...
> 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-lit...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.


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.
Oct 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.