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
#include <iostream>
#include <string>
usingnamespace 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;
}
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.
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".