Unresolved external symbol

Hey guys. Doing my homework and struggling to find out why this error occurs.

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


1
2
3
4
5
6
7
8
9
10
11
template <class CoefType>
class Sparse_Polynomial
{
    ////
};
template <class CoefType>
std::ostream& operator<<(std::ostream& cout, const Sparse_Polynomial<CoefType>& a)
{
    ///
    return cout;
}


Everything was perfect until I added a template. Searched 3 pages of google but none of suggestions were helpful. Most of answers were that compiler can't find definition but it is right after declaration in .h file. Rewrote whole code to make sure it wasn't some random tiny mistake but getting the absolute same error. Maybe I don't pay enough attention? What is wrong with this code? Thanks for any help
The error is from linker. Therefore, your compiler has been happy about your code. You probably should have (after preprocessor):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

template <class CoefType>
class Sparse_Polynomial
{
    ////
};
template <class CoefType>
std::ostream& operator<<(std::ostream& cout, const Sparse_Polynomial<CoefType>& a)
{
    ///
    return cout;
}

int main() {
  Sparse_Polynomial<int> foo; // ok
  std::cout << foo; // ok 
  return 0;
}


That should instantiate and create implementation for
1
2
template <int>
std::ostream& operator<<(std::ostream&, const Sparse_Polynomial<int> &)


If there is no implementation, then the main() has seen only a declaration and assumed that something else provides implementation, but nobody else invokes the operator<<.

right after declaration in .h file

As in?
Alright, looks like I actually didn't pay enough attention. operator<< was a friend function for Sparse_Polynomial and I didn't declare it before the class. Sorry I didn't inculde this. The fact that it worked without templates kinda mislead me. Now this works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class CoefType>
class Sparse_Polynomial;

template <class CoefType>
std::ostream& operator<<(std::ostream& cout, const Sparse_Polynomial<CoefType>& a);

template <class CoefType>
class Sparse_Polynomial
{
	friend std::ostream& operator<< <> (std::ostream&, const Sparse_Polynomial<CoefType>&);
        ////
};
template <class CoefType>
std::ostream& operator<<(std::ostream& cout, const Sparse_Polynomial<CoefType>& a)
{
       ////
}

But why I didn't need operator<< declaration before class without templates and this
1
2
3
4
5
6
7
8
9
class Sparse_Polynomial
{
      friend std::ostream& operator<<(std::ostream&, const Sparse_Polynomial&);
      ///
};
std::ostream& operator<<(std::ostream& cout, const Sparse_Polynomial& a)
{
      ////
}

worked fine?
Topic archived. No new replies allowed.