我写了一个游戏并得到了一个我不明白的编译错误

时间:2015-03-03 20:16:50

标签: c++

我编写了一个程序并获得以下编译器消息:

1>  main.cpp
1>d:\c++ projekte\erebos_2\erebos_2\mainplayer.h(8): error C2146: Syntaxfehler: Fehlendes ';' vor Bezeichner 'Name'
1>d:\c++ projekte\erebos_2\erebos_2\mainplayer.h(8): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
1>d:\c++ projekte\erebos_2\erebos_2\mainplayer.h(8): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
1>d:\c++ projekte\erebos_2\erebos_2\main.cpp(11): error C2144: Syntaxfehler: 'int' sollte auf ';' folgen
1>  Mainplayer.cpp
1>d:\c++ projekte\erebos_2\erebos_2\mainplayer.h(8): error C2146: Syntaxfehler: Fehlendes ';' vor Bezeichner 'Name'
1>d:\c++ projekte\erebos_2\erebos_2\mainplayer.h(8): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
1>d:\c++ projekte\erebos_2\erebos_2\mainplayer.h(8): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.

这是我的代码:

#include <iostream>
#include "windows.h"
#include "MainMenu.h"
#include <ctime>
#include <string>
using namespace std;

void MainMenu:: createMenu()
{
    int typed;
    system("cls");
    cout << "1) Start" << endl;
    cout << "2) Hilfe" << endl;
    cout << "3) Credits" << endl;
    cout << "4) Beenden" << endl;
    cin >> typed;
    if(typed == 1)
    {
        //main.mainGame();
        cout << "...still..." << endl;
    }
}

    class MainMenu
    {
    public:
        void createMenu();
    };

#include <iostream>
#include "windows.h"
#include "MainMenu.h"
#include "MainPlayer.h"
#include <ctime>
#include <string>

using namespace std;

int health;
int xp;
int lvl;
int gold;
string Name = "Namenloser";

void Mainplayer:: showData()
{
    cout << "-----------------------" << endl;
    cout << "Name: " << Name << endl;
    cout << "Leben: " << health << "/100" << endl;
    cout << "Erfahrung: " << xp << "/" << lvl*10 << endl;
    cout << "Level: " << lvl << "/50" << endl;
    cout << "Gold: " << gold << endl;
    cout << "------------------------" << endl;
}

class Mainplayer
{
    public:
        int health;
        int xp;
        int lvl;
        int gold;
        string Name;
        void showData();
};

#include <iostream>
#include "windows.h"
#include "MainMenu.h"
#include "MainPlayer.h"
#include <ctime>
#include <string>
using namespace std;

void maingame()

int main()
{
    system("title EREBOS 2");
    Mainplayer MainChar; 
    MainMenu menu;
    menu.createMenu();
    MainChar.showData();
}

1 个答案:

答案 0 :(得分:2)

MainPlayer.h中,您使用的格式string在此之前未包含在内。在<string>中加入MainPlayer.h

此外,在标头中,使用std::string而不是string指定完整类型。

您的计划中有更多错误,例如在MainPlayer.cpp中,您声明了全局变量,但我想您想要初始化成员变量。这应该发生在构造函数


德语:MainPlayer.h benutzt du den Typ string中,der bis dahin noch nicht包括wurde。在diesem Header中还包括<string>。 Außerdemmusstdu(im Header)std::string statt string schreiben。 Es gibt weitere Fehler,z.B。 deklarierst du globale Variablen in MainPlayer.cpp,obwohl du vermutlich die Membervariablen initialisieren wolltest。在einem Konstruktor geschehen中死亡。