0

Probably a Beginner-mistake so be merciful with me.

I want to access Game.cpp through Game.hpp. But main.cpp seemingly isn't allowing that.

// game.cpp

#include "Game.hpp"

bool loadConfig(std::string config_file)
{
    //does stuff
}

void run()
{
    // does stuff
}
//game.hpp

#include "Interface.hpp"

namespace Oop
{
  class Game
  {
    private:
    Interface& io_;


    public:
        Game(Interface &io);
        bool loadConfig(std::string config_file);
        void run();

  };

  inline Game::Game(Interface &io): io_(io) {};
}
//main.cpp

#include "Interface.hpp"
#include "Game.hpp"

int main()
{
    std::string config_file = "config01.json";

    Oop::Interface io;
    Oop::Game game(io);
    if (!game.loadConfig(config_file))
    {
      return Oop::RETURN_ERROR_BAD_CONFIG;
    }
    game.run();
  }

I've shortened the code so that you can better see what's going on. I know Interface is missing but I don't think it has something to do with the error.

I get the Error message:

  1. Undefined reference to Oop::Game::loadConfig(...)
  2. Undefined reference to Oop::Game::run()

What's the problem?

Thanks in advance.

Quotenbanane
  • 263
  • 1
  • 6
  • 16
  • The most likely reason for that error is that you forgot to inlcude Game.cpp in your build. – R Sahu Apr 24 '20 at 21:29
  • 2
    The issue is that `Game.cpp` defines `loadConfig` and `run` *outside* of any namespace and outside of any class. The definitions should be `bool Oop::Game::loadConfig(std::string config_file) { ... } void Oop::Game::run() { ... }`. – HTNW Apr 24 '20 at 21:32
  • @HTNW thank you my life-saver :-) – Quotenbanane Apr 24 '20 at 21:33

0 Answers0