C++ 架构x86_64的未定义符号:编译错误

C++ 架构x86_64的未定义符号:编译错误,c++,compiler-errors,C++,Compiler Errors,我有一个main.cpp文件,其中有一行: #include "Player.hpp" 以下是Player.hpp: class Player { private : int type; // [0 = humain local, 1 = IA de niveau 1, 2 = IA de niveau 2, 3 = humain en réseau] int score; int color; public : Player(); Player (int type,

我有一个main.cpp文件,其中有一行:

#include "Player.hpp"
以下是Player.hpp:

class Player {
 private :
  int type; // [0 = humain local, 1 = IA de niveau 1, 2 = IA de niveau 2, 3 = humain en réseau]
  int score;
  int color;
 public :
  Player();
  Player (int type, int color);

  int getType();
  int getScore();
  int getColor();
};
下面是Player.cpp:

 Player::Player() {
  }

  Player::Player(int type, int color) {
    this->type = type;
    this->color = color;
    this->score = 0;
  }

  int Player::getType() {
    return this->type;
  }
  int Player::getScore() {
    return this->score;
  }
  int Player::getColor() {
    return this->color;
  } 
如果我用这一行编译:

g++ Main.cpp -o main
它向我显示了错误:

Undefined symbols for architecture x86_64:
  "Player::getColor()", referenced from: etc...........
但如果我将Player.cpp中的代码放在Player.hpp中的代码下面,如下所示:

 class Player {
     private :
      int type; // [0 = humain local, 1 = IA de niveau 1, 2 = IA de niveau 2, 3 = humain en réseau]
      int score;
      int color;
     public :
      Player();
      Player (int type, int color);

      int getType();
      int getScore();
      int getColor();
    };

 Player::Player() {
  }

  Player::Player(int type, int color) {
    this->type = type;
    this->color = color;
    this->score = 0;
  }

  int Player::getType() {
    return this->type;
  }
  int Player::getScore() {
    return this->score;
  }
  int Player::getColor() {
    return this->color;
  } 
它起作用了

我能做些什么来解决这个问题?我认为这是一个汇编问题


谢谢你的帮助

Main.cpp有对Player.hpp的引用,但没有对Player.cpp的引用。添加行:

#include "Player.cpp"
应该解决这个问题

您的命令在这里

g++ Main.cpp -o main
不编译或链接Player.cpp文件,因此它正确地表示在Player.cpp文件中找不到它所说的符号

将Player.cpp添加到构建命令:

g++ Main.cpp Player.cpp -o main

您拥有的每个.cpp文件都构成一个编译单元。它们需要显式指定给编译器。*.hpp可以包含在.cpp文件中,而不需要显式指定给编译器。但是,它们需要位于指定的include目录中。您将看到,随着项目变得越来越大,并且越来越多地分布在多个源文件中,您将需要使用build meckanism(如make甚至CMake)来自动调用编译器。