0

I get the following error message when I try to invoke Game::play() member function, I've been searching for what could be the error. But I couldn't find it.

Here's Game.h

#pragma once

#include <iostream>
class Game {
    public:
    
    

    void selectPlayer();

    // Player* nextPlayer() const;

    bool isRunning() const;

    void play();

    void announceWinner();

};

Game.cpp implementation

#include "Game.h"

void Game::selectPlayer() {

}

bool Game::isRunning() const {

}

void Game::play() {
    // while (isRunning()) {
    //     board.display();
        
    // }
    //board.display();
    std::cout << "hello\n";
}

void Game::announceWinner() {
    std::cout << "Game is over\n";
}

main.cpp

#include <iostream>
#include "Game.h"

int main()  {
    Game g;
    g.play();
    
}

I get this error when I invoke play(): undefined reference to Game::play()'`

  • Most likely you are not building all of your files. If you are using VSCode remember that by default it builds only the active file. You need to edit your `tasks.json` to have it build more than that. Or use the CMake or Makefile tools extensions which you may want to use anyways once you have more than a single source file. The VSCode documentation tells you how to support more than 1 source file here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Nov 24 '21 at 20:23

1 Answers1

0

At a glance your code looks okay. I would suggest adding a class constructor to your .h file:

#pragma once
#include <iostream>

class Game {
    public:

    Game(void);

    ...

Also ensure you have return 0; at the end of your main still.

Zen
  • 115
  • 7