-1

I am new to templates and I have searched the web for this error but I don't know how to fix it it. Already checked Why can templates only be implemented in the header file?

State.h

template <class entityType>
class State
{
public:
    State() = default;
    virtual void Enter(entityType * owner);

};

EnterMine.h

#include "State.h"

class Miner;
class EnterMine : public State<Miner>
{
public:
    EnterMine() = default;
    virtual void Enter(Miner *) {

    };
};

and Miner.cpp is blank

and the problem appears in main.cpp

#include "EnterMine.h"

int main()
{
    EnterMine a;
}

The error I get is a linking error :

LNK2001 unresolved external symbol "public: virtual void __thiscall State::Enter(class Miner *)" (?Enter@?$State@VMiner@@@@UAEXPAVMiner@@@Z)

nickk2002
  • 49
  • 1
  • 6
  • 3
    Where is `EnterMine::EnterMine()` defined? – Evg Nov 19 '19 at 16:05
  • it is not defined. Should I define it?.I tried defining it explicitly but the error didn't dissapear. I also tried inheriting from State like so : EnterMine::EnterMine() : State() { } – nickk2002 Nov 19 '19 at 16:06
  • 2
    Please don't modify the code in your question, because then comments and answers no longer make sense. You can add code, but make it clear that code was added later and was not part of the original question. – Evg Nov 19 '19 at 16:21
  • "_The error I get is a linking error_" OK, and what is your linker config and resulting command then? – underscore_d Nov 19 '19 at 16:21
  • @pbn That's not how static initialisation works. The initialising expression will only be evaluated and assigned to the static object during the first call to the function. – underscore_d Nov 19 '19 at 16:29
  • @underscore_d Indeed. Must have overlooked the static modifier there. – pbn Nov 19 '19 at 16:32
  • 1
    And where is `State::Enter` now defined? – Evg Nov 19 '19 at 16:36
  • Why should i define it anyway? – nickk2002 Nov 19 '19 at 16:37
  • @pbn sorry for modifying the question so many times. – nickk2002 Nov 19 '19 at 16:38
  • 1
    Because it is used somewhere in your code. Maybe you meant a pure virtual function? Then write `virtual void Enter(entityType * owner) = 0;`. – Evg Nov 19 '19 at 16:39

2 Answers2

3

(Note: this answer was written for the original question, it has been completely rewritten after that.)

Every function that is declared and used, should be defined somewhere.

It seems that you declare EnterMine::EnterMine() but never define it. If this constructor does nothing, either omit it (it will be implicitly defined by a compiler), or mark it as = default;.

class EnterMine : public State<Miner>
{
public:
    EnterMine() = default;
    ...
};

This also applies to the State::State() constructor.

Evg
  • 25,259
  • 5
  • 41
  • 83
  • could be related to Miner class? – nickk2002 Nov 19 '19 at 16:26
  • @nickk2002, add the new error to your question. If you implemented constructors correctly and `.cpp` files are included into your project, you should not get the same unresolved external error. – Evg Nov 19 '19 at 16:27
0

Even though it's a singleton, you're still calling the constructor. Thus, you will still need to define the constructor.

In fact, you need to define every function you declare in your header.