好的,感谢大家的帮助,这是我能想到的所有相关内容:
游戏管理器.h"
#include "item.h"
#include "hero.h"
class gamemanager
{
public:
void acquireItems(hero player, item vendor);
};
游戏管理器.cpp:
void gamemanager::acquireItems(hero player, item vendor)
{
int choice;
int choice2;
cout<<"\nWould you like to buy 1.offense OR 2.defense : ";
cin>>choice;
if(choice==1)
{
cout<<"\nGood day sir, what can i do for you: \n1.Buy\n2.Sell\n3.Leave \n";
cin>>choice2;
if(choice2==1)
{
cout<<"\nThese are my wares today: "<<endl;
int index;
int select;
int wep;
char sel;
index=rand()%vendor.Wname.size();
if(index==0)
++index;
for(wep=0; wep<index; wep++)
{
select = rand()%vendor.Wname.size();
cout<<wep<<". "<<vendor.Wname[select]<<endl;
}
cout<<"\nEnter the number of item you want, or enter 'q' to exit"<<endl;
cin>>sel;
if(sel=='q')
return;
weapon* WEAPON = new weapon(vendor.Wname[wep]);
player.inventory.push_back(WEAPON);
player.setdefense();
}
}
英雄.h
class hero
{
public:
vector<item*> inventory;
void setdefense();
};
英雄.cpp
void hero::setdefense()
{
if(inventory.size()!=0)
{
for(unsigned int x=0; x<inventory.size(); x++)
{
m_defense = m_defense + inventory[x]->getbonus();
}
}
}
主要的():
#include <iostream>
#include "gamemanager.h"
#include "hero.h"
#include "enemy.h"
#include "item.h"
int main()
{
gamemanager boss;
item vendor;
hero player(10, 20);
boss.acquireItems(player, vendor);
cout<<player.inventory.size()<<endl;
return 0;
}
课程显然还有很多,但为了规模,我拿出了不相关的东西。就像我说的,当我在 main() 中计算库存大小时,它打印出零,玩家也是在 main() 中创建的类 hero 的实例