我有一个 AdventureGame 类,它有一个构造函数。当我尝试创建一个新的 AdventureGame 对象时,我收到错误消息“没有匹配函数来调用‘AdventureGame::AdventureGame()’
这是我的一些类、构造函数和 main。
#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 游戏(3,5);
关于c++ - 尝试实例化新对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645434/