c++ - 尝试实例化新对象时出错

标签 c++ class struct instantiation

我有一个 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/

相关文章:

python - C++ 中足够大的 ProblemSize 需要 0 运行时间

c++ - 通过 CMake 在 Xcode 4.2 项目中使用 C++0x

java - 通过第二类和第三类更改类变量

c++ - 为某些函数创建类 const 的一些私有(private)成员

c++ - 对同一数据有 8 位和 16 位 View ?

c - 在C中为结构的属性设置默认值

c++ - 提神气递归

c++ - 为介子设置正确的编译器

java - 嵌套类、内部类

c - C 段错误中的链表