Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Operator overloading and inheritance with template classes. (http://www.go4expert.com/articles/operator-overloading-inheritance-t4257/)

huckberry 9Nov2010 22:58

Re: Operator overloading and inheritance with template classes.
Excuse me for poking a really old thread, but this is the most complete reference to template operator overloading I've found to date. My situation is very similar to the one in the example, except that instead of using Derived<T>& as the right hand side of the operator<<, I'm using Derived<T>::SubClass&. The error I'm getting from my compiler (M$ Visual Studio 2010) is this:


LinkedList.obj : 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> > &,struct LinkedList<int>::Element const &)"  referenced in function _main
1>LinkedList.exe : fatal error LNK1120: 1 unresolved externals

Here's the relevant definitions:

template <class Type> class LinkedList {
        struct Element{
                friend ostream& operator<< ( ostream &output, const typename Element &E);

template <typename Type>
ostream& operator <<(ostream& output, const typename LinkedList<Type>::Element& E){

I've changed the definition of friend ostream& opertor << to include the <> however then I receive a different error, which makes me think that Visual Studio wont allow this type of function:


linkedlist.cpp(38): error C2785: 'std::ostream &operator <<(std::ostream &,const LinkedList<Type>::Element &)' and '<Unknown>' have different return types
Any Idea why Visual Studio is comparing it to '<Unknown>'?

huckberry 11Nov2010 00:14

Re: Operator overloading and inheritance with template classes.
Again, My apologies for poking an old thread.

The issue was (eventually) resolved by removing the class Element from within the class LinkedList and making Element a template class.

For future readers, Visual studio requires 'typename' in declarations and allows for the '<>' mentioned prior.


template < class Type > class Element {
    Type Data;
    friend ostream& operator << <>( ostream &output, const typename Element<Type> &E);

template < class Type > class LinkedList {
    Element<Type>&    operator[](int index);        //For Left Hand Values
    const Element<Type>& operator[](int index) const; //For Right Hand Values

ostream& operator<<( ostream &output, const typename Element<Type>& E){
    return output << E.Data;

template <class Type> const typename Element<Type>& LinkedList<Type>::operator[](int index) const{
    Element<Type> Answer;
    //Logic Here
    return *Answer

int main(){
    LinkedList<int> Array1;
    //init and add memebers
    cout << Array1[0];
    return 0;

jpope19 9Mar2012 05:03

Re: Operator overloading and inheritance with template classes.
jogojapan just saved me a lot of time. thanks bro!

All times are GMT +5.5. The time now is 20:58.