0

I have two templated overloaded friend functions in my Set class that keep sending back the error

Templatedriver.cpp:(.text+0x2a0): undefined reference to `std::ostream& operator<< <int>(std::ostream&, Set<int> const&)'

and

Templatedriver.cpp:(.text+0x2dd): undefined reference to `std::ostream& operator<< <int>(std::ostream&, Set<int> const&)'

and so on(simply changing the out for double, char, etc.) I have searched this up and the most common response on here is to do a forward declaration then declare it as a friend in the class, I have done this and still get an undefined reference

this is my forward declaration

template <class T> istream& operator>>(istream&, Set<T>&);
template <class T> ostream& operator<<(ostream&, const Set<T>&);

and I declare them in them in the class as such

template <class T>
class Set {

friend istream& operator>> <>(istream&, Set<T>&); 
template <class Y> */friend ostream& operator<< <>(ostream&, const Set<T>&);
....//rest of class
}

and these are the definitions

template <class T> istream& operator>>(istream& is, Set<T>& S){
    S.input();
    return(is);
}


template <class T> ostream& operator<<(ostream& os, const Set<T>& S){
        S.display();
        return(os);   
}

Theres a few more overloaded classes im having trouble with but it is all the same problem

  • Are the definitions in the header file? – user253751 May 16 '16 at 01:36
  • immibis's question is important. If your instantiation of `Set` can't see the `operator<<` and `operator>>` implementations, then they never get instantiated. – md5i May 16 '16 at 01:51
  • no, the definitions are in Set.cpp should they be in the header file then? – John Smith May 16 '16 at 01:54
  • Templated functions should only be defined in the implementation file (cpp) if they are only used in that file. Otherwise, they need to be in the header. This is because a file that uses type `X` needs the template definition to instantiate an instance that works on type `X`. – md5i May 16 '16 at 01:57
  • 1
    There's your problem. The definitions must be in the header file. This is a dupe of http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Sam Varshavchik May 16 '16 at 01:57
  • Sam Varshachik, sorry about that relatively new to coding and didnt think to to look that up. md5i Thanks for the help/explanation i appreciate it! – John Smith May 16 '16 at 02:11

1 Answers1

0

In Set, I think you want to use a templated friend declaration:

template <typename T>
friend istream& operator>> (istream&, Set<T>&);

This declares that any function that matches after template parameter substitution is a friend of the class.

(You may have almost had this, but I was confused by the */ in the code. Suffice it to say that you should remove the <>.)

md5i
  • 3,018
  • 1
  • 18
  • 32
  • ah damn I meant to delete that on here, the stuff right before the */ should not be there and I did that at one point and it didnt work – John Smith May 16 '16 at 01:54