Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:未定义对';player()';_C++_Reference_Header_Syntax Error_Undefined - Fatal编程技术网

C++ 错误:未定义对';player()';

C++ 错误:未定义对';player()';,c++,reference,header,syntax-error,undefined,C++,Reference,Header,Syntax Error,Undefined,程序员们!我希望进入游戏开发领域,所以我正在尝试编写我自己的,非常简单的文本战斗模拟器。你可以选择一个玩家名字,并与你选择的怪物战斗。不管怎样,我的目标是先编写简单的代码,然后扩展并添加更多的类。以下是我目前仅有的两个文件: player.h #ifndef PLAYER_H #define PLAYER_H #include <string> using std::string; class player { public: player(); const int maxH

程序员们!我希望进入游戏开发领域,所以我正在尝试编写我自己的,非常简单的文本战斗模拟器。你可以选择一个玩家名字,并与你选择的怪物战斗。不管怎样,我的目标是先编写简单的代码,然后扩展并添加更多的类。以下是我目前仅有的两个文件:

player.h

#ifndef PLAYER_H
#define PLAYER_H

#include <string>
using std::string;


class player
{
public:
player();

const int maxHealth = 100;
int armorModifier = 0;
int playerLevel = 1;
int gold = 0;
int currentHealth = maxHealth;

string Name;

~player();
};
#endif // PLAYER_H
\ifndef PLAYER\u H
#定义PLAYER_H
#包括
使用std::string;
职业选手
{
公众:
player();
const int maxHealth=100;
整数铠装修改器=0;
int playerLevel=1;
int gold=0;
int currentHealth=maxHealth;
字符串名;
~player();
};
#endif//PLAYER\u H
BattlesMain.cpp

/* GAME FEATURES THAT ARE COMMENTED WILL BE IMPLEMENTED AT A LATER TIME */


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

using std::cout;
using std::cin;

int main()
{
  /* MAIN MENU */

cout << "Monster Battles: Text Action, v0.1\n";
cout << "Welcome, fighter!\n";
cout << "1.New Game\n";
// cout << "2.Load Game\n";
cout << "3.Quit Game\n";

char choice;

cin >> choice;

/* GAME LOOP */

while (choice !='4')
{
   if (choice == '1')
   {
       player Player1;

       cout << "Arena Host: Hello, fighter. What is your name?\n";
       cin >> Player1.Name;
       cout << "Welcome to the arena, " << Player1.Name << ". Here, you will \n";
       cout << "be given the chance to battle fearsome monsters for fortune and \n";
       cout << "fame. With the gold you win, you can visit our shop to buy new weapons, \n";
       cout << "armor and other useful items. Since you are unarmed, here's a THIEF'S DAGGER. \n";
       cout << "It's not much, but you'll hopefully be able to buy better items later! Good luck!\n";


}

return 0;}
/*已注释的游戏功能将在稍后实施*/
#包括
#包括“player.h”
使用std::cout;
使用std::cin;
int main()
{
/*主菜单*/
cout您没有提供构造函数和析构函数的定义(您只有
player
类定义中的声明)

如果构造函数和析构函数不应该做任何事情,就不要显式地声明它们。编译器将隐式地为您生成它们

特别是,用户提供的析构函数具有(我想说,很可能是不希望的)禁止隐式生成移动构造函数和移动赋值运算符的后果(其中,复制构造函数和复制赋值运算符的隐式生成仅在C++11中被弃用)

此外,只有在C++11之后才允许使用初始化变量的方法。如果您想知道如何在C++03中初始化成员变量,可以使用以下方法进行初始化:


当然,您必须在类定义中省略初始值设定项,并且仍然必须包含构造函数的声明。

并且在类声明中也不允许将值赋值给成员变量谢谢您,Andy!这样做了;我的程序按预期运行。Wojo:为什么?如果我想要怎么办要初始化值(如我的玩家的常量最大健康值)?还有其他方法吗?@DimitrisAlmeidaKokkaliaroglo:是的,还有其他方法(例如在构造函数的初始化列表中)然而,在C++ 11中,你所做的是允许知道的。在旧的标准中,我如何初始化构造函数中的变量?我想知道兼容性的原因。@ DimITISAlMeIDAkkkalaRoGoLo:我编辑了答案,以显示。如果这个帖子回答了你的问题,请考虑将答案标记为“接受”:
player::player()
    :
    maxHealth(100),
    armorModifier(0),
    playerLevel(),
    gold(0),
    currentHealth(maxHealth)
{
}