Templates

I have this very simple class template program which I created to better understand templates, but the damn thing wont run because there is an unresolved external?

My whole program checks out, I cannot find any syntax errors

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
33
34
35
36
37
38
#include <iostream>
#include <string>

using namespace std;

template <class T>
class Lol
{
private:
	T one, two;
public:
	Lol(T a, T b)
	{
		one = a;
		two = b;
	}
	T big();
	~Lol();
};
template <class T>
T Lol<T>:: big()
{
	return(one > two ? one : two); //if first bigger than second, return first else second
}

int main()
{
	string one = "lol";
	string two = "lmao";
	Lol <string> Test(one, two);
	Lol <int> Two(59, 20);
	cout << Two.big();
	cout << Test.big();

	system("PAUSE");
	return 0;

}
Provide a definition for your destructor.
lines 32-33. it doesn't know what kind of type the template is
but I declared the object as <string> and <int>???
Don't think its because of the destructor since the compiler handles that by default
My compiler with your code:
Severity	Code	Description	Project	File	Line
Error	LNK2019	unresolved external symbol "public: __thiscall Lol<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > 
>::~Lol<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>
 > >(void)" (??1?$Lol@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main


My compiler with a defined destructor (empty):
59lolPress any key to continue . . .


It also works by removing the declaration of the destructor.

You could also change it to: ~Lol() = default;

You gave a definition for a destructor, even though it was intended to be empty. It will no longer make one for you, just like it will no longer make a constructor for you if you define your own.

http://www.codesynthesis.com/~boris/blog/2012/04/04/when-provide-empty-destructor/
Last edited on
Interesting, my teacher told me if we define it as ~Lol(); it is the same as not defining it at all and the compiler will go ahead and perform a default destructor
Here's the funny thing. If you put "~Lol();" there, it will say "Oh, they're defining their own.". It will look for your definition. If it fails to find one, it'll vomit. If you want it to just implicitly create one for you (default destructor), you can just omit it, or use = default;

~Lol() = default;

Is this totally stupid? Yes, in my opinion. If you choose to put "~Lol()" in your definition, then you have to provide a definition. Or you could just leave it empty.
1
2
3
~Lol() = {

}


That should work just fine as well. My IDE automatically declares and defines empty constructors and destructors for me, but many don't do that for you. I usually just leave them empty, then I can do add to them if I need to, or I'll use "= default".
Ah I see, so its useless to define the destructor but then put = default, since you might as well not put it at all right?

Cheers for clearing it up!
Topic archived. No new replies allowed.