5

I'm trying to compile a very basic C++ program and there is something wrong. Whatever it is, I'm sure it's very obvious. I have three very short files.

main.cpp:

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

using namespace std;

int main()
{
    Player rob;
    cout << "Iran" << endl;
    return 0;
}

Player.h

#ifndef PLAYER_H
#define PLAYER_H

class Player {
public:
    Player();
private:
    int score;
};

#endif

Player.cpp

#include "Player.h"

Player::Player(){
    score = 0;
}

The command I'm using to compile is g++ main.cpp -o main And the error I'm being issued by the compiler is:

/tmp/ccexA7vk.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `Player::Player()'
collect2: error: ld returned 1 exit status

Note: All these files are in the same directory.

Quinn McHugh
  • 1,577
  • 2
  • 17
  • 23
  • 4
    Try compiling the `player.cpp` file as well... The compiler cannot automagically just find the function body in non-compiled file. – Xarn Jul 27 '16 at 21:56
  • 5
    Looks like you aren't compiling Player.cpp. Give `g++ main.cpp Player.cpp -o main` a try. – user4581301 Jul 27 '16 at 21:56
  • 4
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Richard Critten Jul 27 '16 at 21:57
  • Ahh yeah you all are correct. I just needed to have the Player.cpp included in there as well. Thanks very much. – Quinn McHugh Jul 27 '16 at 21:58

1 Answers1

8

As mentioned in the comments, you are not feeding Player.cpp into compiler. You should give all the cpp files to the compiler.

g++ main.cpp Player.cpp -o main
Siavoshkc
  • 346
  • 2
  • 16