0
votes

I've got code like this:

#pragma once

#include "MV/stateSystem/StateSystem.hpp"
#include "MV/config/Config.hpp"

namespace mv
{
    class Initializator
    {
        /* ===Objects=== */
    public:
    protected:
    private:
        static Initializator *instance;
        /* ===Methods=== */
    public:
        //Inits the program
        void init();

        static Initializator& getInstance();
        static void createInstance();
    protected:
    private:
        Initializator();
        Initializator(Initializator const& copy) = delete;            // Not Implemented
        Initializator& operator=(Initializator const& copy) = delete; // Not Implemented
    };
}

and .cpp

    #include "Initializator.hpp"


    namespace mv
    {
    Initializator* Initializator::instance;

    void Initializator::init()
    {
        StateSystem::readStatesFromFile("data/states/states.txt");
    }

    Initializator & Initializator::getInstance()
    {
        if (instance == 0)
            Logger::Log(constants::error::singleton::SINGLETON_NOT_INITED, Logger::STREAM::BOTH, Logger::TYPE::ERROR);
        return *instance;
    }

    void Initializator::createInstance()
    {
        if (instance == 0)
            instance = new Initializator();
    }

}

but my compiler has got problem:

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "private: __thiscall mv::Initializator::Initializator(void)" (??0Initializator@mv@@AAE@XZ) referenced in function "public: static void __cdecl mv::Initializator::createInstance(void)" (?createInstance@Initializator@mv@@SAXXZ)

I can't understand it because i've got other class where code is really similar and compiler hasn't got problem with it. In the past, when i've got this problem, i had to declare static members in .cpp file (for example: Initializator* Initializator::instance; ) but now it doesn't help me.

2
What programming language is this? Is it C++? Please tag your question with the language in use. To update your question, click on the "edit" link under the post. Thank you.Pang

2 Answers

1
votes

You are declaring the ctor, but not defining it You can define it within class declaration with

     Initializator() = default;

Or

     Initializator(){};

Or also in the cpp with

     Initializator::Initializator(){}

As the error message is saying, Initializator::createInstance() is calling the ctor, which has to be defined even if it is private

1
votes

Declaring your default ctor as private prohibits the creation of an implicilty defined default ctor. Thus, you get the unresolved external symbol linker error.
To get it working, provide a valid ctor definition in your code.
Either in the .cpp file, or in the .hpp file ( by provding an in-class definition or writing ctor() = default forcing the automatic generation of a default constructor by the compiler ).