error LNK2019: unresolved external symbol (templated functions)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

template <class T>
struct Vehicle 
{ 
    T num = -1; 
    Vehicle(T n) : num(n) {};
    friend std::ostream& operator<<(std::ostream& stream, const Vehicle<T>& b);
};

template <class T>
std::ostream& operator<<(std::ostream& stream, const Vehicle<T>& b)
{
    stream << "[Veh:" << b.num << "]";
    return stream;
}

template <class T>
void Drive(Vehicle<T>& b) 
{
    std::cout << b;
}

int main()
{
    Vehicle<int> car1(1); 
    Vehicle<std::string> car2("Tesla");
    Drive(car1);
    Drive(car2);
    return 0;
}


I get the following compilation error:


1>ex_Templates.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 Vehicle<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABU?$Vehicle@H@@@Z) referenced in function "void __cdecl Drive<int>(struct Vehicle<int> &)" (??$Drive@H@@YAXAAU?$Vehicle@H@@@Z)
1>ex_Templates.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 Vehicle<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABU?$Vehicle@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@@Z) referenced in function "void __cdecl Drive<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(struct Vehicle<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)" (??$Drive@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@YAXAAU?$Vehicle@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@@Z)
1>C:\Users\Fluffy\Desktop\ex_Templates\Debug\ex_Templates.exe : fatal error LNK1120: 2 unresolved externals


I don't know why there's an 'unresolved external symbol' issue since the compiler should've created a Vehicle<int> class, an int version of the ostream operator<< overload and Drive() for car1.

Last edited on
The friend declaration also needs to be templated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
include <iostream>

template <class T>
struct Vehicle
{
	T num = -1;
	Vehicle(T n) : num(n) {};
	template<class T>
	friend std::ostream& operator<<(std::ostream& stream, const Vehicle<T>& b);
};

template <class T>
std::ostream& operator<<(std::ostream& stream, const Vehicle<T>& b)
{
	stream << "[Veh:" << b.num << "]";
	return stream;
}

template <class T>
void Drive(Vehicle<T>& b)
{
	std::cout << b;
}

int main()
{
	Vehicle<int> car1(1);
	Vehicle<std::string> car2("Tesla");
	Drive(car1);
	Drive(car2);
	return 0;
}



[Veh:1][Veh:Tesla]

seeplus wrote:


Ah simple enough. Thanks!
Last edited on
Topic archived. No new replies allowed.