[英]Short version: Error 14 error C2660: 'Player::addSpell' : function does not take 1 arguments
I'm a slow learner to C++ here. 我在这里学习C ++较慢。 My game code is a text based RPG.
我的游戏代码是基于文本的RPG。 Here the player chooses to by a spell and add it to his spell book called 'Grimoire.'
玩家在这里选择一个咒语,并将其添加到名为“ Grimoire”的咒语书中。 Grimoire is a vector type that contains 'Spell' types as elements.
Grimoire是一种向量类型,其中包含“咒语”类型作为元素。 Spell is a struct type that has values such as it's name, damage range, MP, and price.
咒语是一种结构类型,其值包括其名称,伤害范围,MP和价格。 If the code snippet is too vague for the issue concerned see the original question titled:
如果代码片段对于所涉及的问题过于含糊,请参阅标题为:
Error 14 error C2660: 'Player::addSpell' : function does not take 1 arguments 错误14错误C2660:'Player :: addSpell':函数未采用1个参数
The problematic line is: Player.addSpell(mMagic[0]); 有问题的行是: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;
};
Player
is a class and not an object. Player
是一个类,而不是对象。 You need to create an object (an instance) of the Player
class, and use that. 您需要创建
Player
类的对象(实例),然后使用它。
For example: 例如:
Player p;
...
p.addSpell(mMagic[0]);
您需要实例化一个对象(类Player的对象)以调用成员函数(addSpell(Spell))。只有静态成员函数和staic成员变量可以使用类名来调用,因为它们是类特定的,成员函数是对象特定的,因此您需要有一个对象实例来调用它们。
I think your real problem is that you need access to the same Player instance from many different modules. 我认为您真正的问题是您需要从许多不同的模块访问同一Player实例。
Usually for that you should store Player instance in your "game logic" module (that should be easily accessible from anywhere) and implement accessor to that instance. 为此,通常应将Player实例存储在“游戏逻辑”模块中(应该可以从任何地方轻松访问),并实现对该实例的访问器。
Try something like this: 尝试这样的事情:
//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*******
}
For more elaborate/aesthetic ways to handle such problems you should read about Design Patterns and Game Design/Programming Patterns. 有关处理此类问题的更详尽/美观的方法,您应该阅读有关设计模式和游戏设计/编程模式的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.