Welcome! Log In Create A New Profile

Advanced

Problem overriding output operator for template class

Posted by valkeye 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Problem overriding output operator for template class
April 01, 2008 09:14AM
Hi guys,

Has anyone else encountered problems when trying to override the output operator for a template class? The error will only be detected if you actually use the output operator in your main(), otherwise, the error will go by undetected. This is what I've found:


template<class Type>
class Test{
    friend ostream& operator<<(ostream&, Test<Type>& );      
 };

template<class Type>
ostream& operator<<(ostream &out, Test<Type> &obj){
    out << "Hello" << endl;         
    return out;
}

int _tmain(int argc, _TCHAR* argv[])
{   
    Test<int> my;
    cout << my;
    system("pause"winking smiley;
    return 0;
}

This generates an error at compile time:
10 ..... [Warning] friend declaration `std :: ostream& operator<<(std :: ostream&, Test<Type>& )' declares a non-template function 
10 ..... [Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning 
          [Linker error] undefined reference to `operator<<(std :: ostream&, Test<int>& )' 
10 ......ld returned 1 exit status

It seems the only way I have found to get around this is by doing the following:

template<class Type> class Test; <-- new line
template<class Type> ostream& operator<<(ostream&, Test<Type> & ); <-- new line
template<class Type>
class Test{
    friend ostream& operator<< <Type>(ostream&, Test<Type>& ); <-- edited
 };

template<class Type>
ostream& operator<<(ostream &out, Test<Type> &obj){
    out << "Hello" << endl;         
    return out;
}

int _tmain(int argc, _TCHAR* argv[])
{   
    Test<int> my;
    cout << my;
    system("pause"winking smiley;
    return 0;
}

Anybody experienced something similar?

Thanks

-Valkeye
Re: Problem overriding output operator for template class
April 05, 2008 01:15AM
Yup, I was battling with this and Google wasn't being much help.
Well done for finding the fix.
I had to apply it to the linkedList.h file on the MalikDownloads section of the unisa CD, just to get my compile through.

I was starting to feel rather sheepish for using my own preferred environment:
Eclipse C/C++ with msys 1.0 (for make utility)

Thanks,
Neil.
avatar Re: Problem overriding output operator for template class
April 15, 2008 02:19PM
Thanks for this.

I placed all the code in a single file and also split the code into two cpp files with no problems. Only have the problem when using header file.confused smiley

A few more things required than indicated in the tutorial letter.

To clarify, the first section is done in the header file. Copied again below.

template<class Type> class Test; <-- new line
template<class Type> ostream& operator<<(ostream&, Test<Type> & ); <-- new line
template<class Type>
class Test{
friend ostream& operator<< <Type>(ostream&, Test<Type>& ); <-- edited
};

The user file and the implementation file do not change.

Regards
B
Re: Problem overriding output operator for template class
April 19, 2008 01:18PM
It still doesn't work for me. One of the senior students gave me a version that does work, but transferring it to Maliks code crashes everything.

Using my own version of the class I managed to test my function for the first question, but now the derived class code has another 20-30 errors. Handing in an assignment without testing the code seems stupid, but I guess there is no help for it if the author doesn't know what he's doing to begin with.

I wonder how much Unisa was paid to take this text book?
Re: Problem overriding output operator for template class
April 20, 2008 03:28PM
template<class Type> class linkedListType;
template<class Type> ostream& operator<<(ostream&, linkedListType<Type> & );


template<class Type>
class linkedListType
{
friend ostream& operator<<<Type>(ostream&, const linkedListType<Type>&winking smiley;

Hi, This is my amended code but I still get an error:
error C:\unisa\Cos211x\src\MalikDownloads\Chapter 5 Source Code\linkedListType\linkedList.h:21
template-id `operator<< <int>' for `std::basic_ostream<char, std::char_traits<char> >& operator<<(std::basic_ostream<char, std::char_traits<char> >&, const linkedListType<int>&winking smiley' does not match any template declaration


I have tried <> instead of <Type> but get the same error. Any more ideas?
Re: Problem overriding output operator for template class
April 29, 2008 06:30AM
Hi people

Please help, I am still not coming right.

I have added the extra lines etc, as shown by valkeye, yet I am getting the exact same compile error described by paulette above. I also get an error message saying that first is protected, reffering to the line

current = list.first;

in the definition of the overloaded << operator.

For the meantime I have added a member function print to output my lists, but this is obviously less than ideal. If you can see where I have gone wrong please help.

p.s. My declarations and definitions are in the same file (cpp) and main() in another cpp file. Works fine if I use the print function and comment out the declaration to overload the << operator
avatar Re: Problem overriding output operator for template class
May 04, 2008 05:24PM
Does anyone have a full working version of the OrderedLinkedList and the LinkedList? I've tried near everything and cannot get this to work. I retyped everything twice and reinstalled two versions of DevCpp but to no avail. Then I tried using the code from the CD and got even more errors.

When typing the code from the book I managed to fix everything only to keep getting errors about "first", "last" and "count" not existing. But there's nothing wrong with the project setup because some older projects that inherited protected members do find them.

I'm quite desperate to get this working because I don't want to keep working with non-working programs and I don't have the time now to do a LinkedList implementation in my own way.

If anyone can help, email me: malberts [at] mtnloaded [dot] co [dot] za

Thanks
Sorry, only registered users may post in this forum.

Click here to login