简体   繁体   English

.cpp 文件在编译时看不到.h 文件

[英].cpp file not seeing .h file at compile time

I am trying to compile a unit for a class, which I have split into a .hpp and .cpp file.我正在尝试为 class 编译一个单元,我已将其拆分为.hpp.cpp文件。 The files are called player.hpp and player.cpp .这些文件称为player.hppplayer.cpp

I used the command g++ -c player.cpp .我使用了命令g++ -c player.cpp

Here is player.hpp这是player.hpp

#define PLAYER
#ifndef PLAYER

#include <string>
#include <iostream>

class Player
{
    private:
        char symbol;
        std::string name;
    public:
        Player(char symbol, std::string name);
        std::string makeMove(); // Prompts the player to input a move to make
}

#endif            

and here is player.cpp这是player.cpp

#include "player.hpp"

Player::Player(char symbol)
{
    this->symbol = symbol;
    this->name = name;
}

std::string Player::makeMove()
{
    std::string playerMove;
    std::cout << this->name << "'s turn.\nPlease make your move (enter as number combination of row and column. ex: 13 = First row, third column)\n";
    std::cin << playerMove;
    return playerMove;
}         

Here is the output这是 output

player.cpp:3:1: error: ‘Player’ does not name a type
    3 | Player::Player(char symbol)
      | ^~~~~~
player.cpp:9:6: error: ‘string’ in namespace ‘std’ does not name a type
    9 | std::string Player::makeMove()
      |      ^~~~~~
player.cpp:2:1: note: ‘std::string’ is defined in header ‘<string>’; did you forget to ‘#include <string>’?
    1 | #include "player.hpp"
  +++ |+#include <string>
    2 | 

It would seem that my .hpp file is not being included properly, why?似乎我的.hpp文件没有被正确包含,为什么?

In your player.hpp , the order of lines in the include guard is incorrect.在您的player.hpp中,包含守卫中的行顺序不正确。 It should be:它应该是:

#ifndef PLAYER
#define PLAYER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM