unresolved external symbol
I have written a code and there is an error witch I could not fix.
The code:
triangle.h
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
using namespace std;
template<typename T>
class triangle
{
T side1,side2,side3 ;
public:
triangle() {}
triangle(T x,T y,T z):side1(x),side2(y),side3(z) {}
bool valid();
friend ostream& operator<< (ostream& ostr, const triangle<T>& t);
};
|
triangle.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include "triangle.h"
#include <iostream>
using namespace std;
template<class T>
bool triangle<T>::valid()
{
if(!(first>0 && second>0 && third>0))
return false;
if(third + second > first)
return false;
if(third + first > second)
return false;
if(first + second > third)
return false
return true;
}
template <typename T>
ostream& operator<< (ostream& ostr, const triangle<T>& t)
{
ostr<<'('<<t.side1<<','<<t.side2<<','<<t.side3<<')';
return ostr;
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include "triangle.h"
#include <stdlib.h>
using namespace std;
void main()
{
triangle<int> t(5,2,3);
cout << t;
system("PAUSE");
}
|
The error
|
main.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> > &,class triangle<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$triangle@H@@@Z) referenced in function _main
|
I have been trying to solve this for about a week now, no luck.
Any takers?
Last edited on
template function bodies cannot be defined in a separate cpp file. You must put them in the header file.
Get rid of triangle.cpp and put that in triangle.h
EDIT:
Beware of those error witches. Their dark magic is very deadly!
</wiseguy>
Last edited on
Disch wrote: |
---|
"template function bodies cannot be defined in a separate cpp file. You must put them in the header file." |
True. However, explicitly instantiated templates are allowed, because they are complete types.
Header:
1 2 3 4 5 6
|
template <typename T>
struct Structure
{
Structure(T const &Init = T());
T Member;
};
|
Source:
1 2 3 4 5 6
|
template struct Structure<int>; // Explicit Instantiation
template <>
Structure<int>::Structure(int const &Init)
: Member(Init)
{ }
|
Wazzak
Thank you Disch and Framework. It has solved the problem! Like dark magic!
Yes @ Framework. I was just trying to keep my response simple =P
Topic archived. No new replies allowed.