简短版本:错误14错误C2660:' Player :: addSpell' :function不带1个参数

时间:2015-01-29 09:05:28

标签: c++

我在这里慢慢学习C ++。我的游戏代码是基于文本的RPG。在这里,玩家通过咒语选择并将其添加到他的法术书“Grimoire”中。 Grimoire是一种矢量类型,包含“拼写”类型作为元素。法术是一种结构类型,具有诸如名称,伤害范围,MP和价格等值。如果代码段对于相关问题过于模糊,请参阅标题为的原始问题:
错误14错误C2660:'Player :: addSpell':函数不带1个参数

有问题的行是:Player.addSpell(mMagic [0]);

//Store.cpp
      ...       ...       ...

                    //If player already has the spell
                    if (Player.getSpellName() == mMagic[0].mSpell)
                    {
                        cout << "You already have this spell." << endl << endl;
                        break;
                    }
                    else
                    {
                        //Increase size of Grimoire to store the purchased spell
                        Player.increaseGrimoire();

                        //Add spell to Grimoire

                        Player.addSpell(mMagic[0]); //HERE IS THE PROBLEM*******


//Player.H
class Player
{
public:
    //Constructor.
    Player();

    //Methods

    void addSpell(Spell magic);

private:
    //Data members.
vector<Spell>mGrimoire;

};


//Player.cpp
void Player::addSpell(Spell magic) 
{
    int i = 0;
    for (; i < mGrimoire.size(); ++i)
    {
        if (mGrimoire[i].mSpell == magic.mSpell)
            continue;
        else if (!(mGrimoire[i].mSpell == magic.mSpell))
        {
            mGrimoire[i] = magic;
            break;
        }

    }
}

//Store.h
 struct Store
{
public:

private:

    vector<Spell>mMagic;

};

3 个答案:

答案 0 :(得分:1)

Player ,而不是对象。您需要创建Player类的对象(实例),然后使用它。

例如:

Player p;

...

p.addSpell(mMagic[0]);

答案 1 :(得分:0)

你需要实例化一个对象(类Player的对象)才能调用成员函数(addSpell(Spell))。只能使用类名调用静态成员函数和staic成员变量,因为它们是特定于类的,成员函数是特定于对象的,因此您需要让对象实例化以调用它们。

答案 2 :(得分:0)

我认为您真正的问题是您需要从许多不同的模块访问相同的Player实例。通常,您应该将Player实例存储在您的游戏逻辑&#34;模块(应该可以从任何地方轻松访问)并实现该实例的访问者。

尝试这样的事情:

//Game.h
class Game
{
    ...
    private:
        static Game *instance;
        Player *player;
    public:
        static Game* getInstance();
        Player *getPlayer();
        Game::init();
}

//Game.cpp
Game* Game::instance = NULL;
Game* Game::getInstance()
{
    return instance;
}

void Game::init()
{
    ...
    instance=this;
    ...
    player = new Player();
    ...
}

Player* Game::getPlayer()
{
    return player;
}

//Store.cpp
...
    Player* player=Game::getInstance()->getPlayer();
    //If player already has the spell
    if (player.getSpellName() == mMagic[0].mSpell)
    {
        cout << "You already have this spell." << endl << endl;
        break;
    }
    else
    {
        //Increase size of Grimoire to store the purchased spell
        player.increaseGrimoire();

        //Add spell to Grimoire

        player.addSpell(mMagic[0]); //HERE IS THE PROBLEM*******
    }

有关处理此类问题的更精细/美学方法,您应该阅读有关设计模式和游戏设计/编程模式的内容。