我有一個AdventureGame類,它有一個構造函數。當我嘗試做一個新AdventureGame對象,我收到錯誤消息「沒有匹配的函數調用‘AdventureGame :: AdventureGame()’嘗試實例化新對象時出錯
下面是我的一些類,構造函數,和主。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class AdventureGame
{
private:
public:
int playerPos;
int ogrePos;
int treasurePos;
string location;
AdventureGame(int ogre, int treasure)
{
playerPos = -1;
ogrePos = ogre;
treasurePos = treasure;
location = "";
};
.
.
. // other functions that I'm sure are irrelevant
.
.
int main()
{
AdventureGame game;
int numMoves = 0;
std::string move;
while (!game.isGameOver(game.playerPos))
{
game.printDescription(game.playerPos);
cout << "Which direction would you like to move? (forward, left, or right)" << endl;
cin >> move;
game.move(move);
numMoves++;
}
}
如何創建一個新的遊戲
再次讀取錯誤。你在類中有一個顯式的構造函數,簽名爲'AdventureGame(intogog,intbauer)'。這將除去編譯器自動生成的默認構造函數。所以要麼你必須刪除顯式構造函數,或者在代碼中調用顯式構造函數而不是默認構造函數。看看:http://en.cppreference.com/w/cpp/language/default_constructor – nakiya 2014-10-30 05:05:05