好的,我有MainShop
(基类),然后SwordShop
和BowShop
(派生类)和另一个名为Inventory
的派生类。我的基类中有一个受保护的向量,当我从派生类访问它时,它没关系,但每个都有自己的值。如何将它们全部设置为具有相同的值?
//MainShop.h
#pragma once
class MainShop
{
private:
//some variables
protected:
vector <string> WeaponInventory;
public:
//Some functions not related to the vector
};
//MainShop.cpp
/*Implementation of functions*/
//SwordShop.h
#pragma once
#include "MainShop.h"
class SwordShop: public MainShop
{
private:
int choice;
public:
void getSwordShop();
void setWeaponSoldier(int i, string s);
void soldierShop();
};
//SwordShop.cpp
#include "SwordShop.h"
void SwordShop::soldierShop()
{
this->setWeaponSoldier(1, "1) Meito Ichimonji\n +4 Damage\n 150Gold");
this->setWeaponSoldier(2, "2) Shusui\n +10 Damage\n 230Gold");
this->setWeaponSoldier(3, "3) Elixir\n +16 Damage\n 300Gold");
this->setWeaponSoldier(4, "4) Blade of scars\n +24 Damage\n 550Gold");
this->setWeaponSoldier(5, "5) Ragnarok\n +32 Damage\n 610Gold");
this->setWeaponSoldier(6, "6) Eternal Darkness\n +40 Damage\n 690Gold");
this->setWeaponSoldier(7, "7) Masamune\n +52 Damage\n 750Gold");
this->setWeaponSoldier(8, "8) Soul Calibur\n +60 Damage\n 900Gold");
this->getSwordShop();
cout << "What would you like to buy?";
cin >> choice;
switch (choice)
{
case 1:
WeaponInventory.push_back("Meito Ichimonji");
cout << "You have Successfully Bought Meito Ichimonji\nIt has been added to your inventory\n";
break;
case 2:
WeaponInventory.push_back("Shusui");
cout << "You have Successfully Bought Shusui\nIt has been added to your inventory\n";
break;
//ETC
default:
cout << "Error! You have entered an invalid answer\nPlease try again";
this->soldierShop();
}
cout << "your total items are: "<< WeaponInventory.size();
好的,我想说我买了两件物品。这里显示我有2个项目。但如果我在Inventory.cpp中cout << "your total items are: "<< WeaponInventory.size();
,我会说我有0!那是我的问题。
void SwordShop::getSwordShop()
{
//Display Choices
for (map<int, string>::iterator iter = WeaponSoldier.begin(); iter != WeaponSoldier.end(); iter++)
{
cout << iter->second << endl;
cout << endl;
}
}
void SwordShop::setWeaponSoldier(int i, string s)
{
WeaponSoldier[i] = s;
}
//BowShop.h
#pragma once
#include "MainShop.h"
class BowShop: public MainShop
{
private:
int choice2;
public:
void getBowShop();
void setWeaponArcher(int i, string s);
void ArcherShop();
};
//BowShop.cpp
#include "BowShop.h"
void BowShop::ArcherShop()
{
BowShop::setWeaponArcher(1,"1) Arondight\n +4 Damage\n 150Gold");
BowShop::setWeaponArcher(2,"2) Gugnir\n +10 Damage\n 230Gold");
BowShop::setWeaponArcher(3,"3) Susano'\n +16 Damage\n 300Gold");
BowShop::setWeaponArcher(4,"4) Longinus\n +24 Damage\n 550Gold");
BowShop::setWeaponArcher(5,"5) Hrunting\n +32 Damage\n 610Gold");
BowShop::setWeaponArcher(6,"6) Clarent\n +40 Damage\n 690Gold");
BowShop::setWeaponArcher(7,"7) Shinigami\n +52 Damage\n 750Gold");
BowShop::setWeaponArcher(8,"8) Caliburn\n +60 Damage\n 900Gold");
this->getBowShop();//Display options
cout << "What would you like to buy?";
cin >> choice2;
switch (choice2)
{
case 1:
WeaponInventory.push_back("Arondight");
cout << "You have Successfully Bought Arondight\nIt has been added to your inventory\n";
break;
case 2:
WeaponInventory.push_back(" Gugnir");
cout << "You have Successfully Bought Gugnir\nIt has been added to your inventory\n";
break;
//ETC
default:
cout << "Error! You have entered an invalid answer\nPlease try again";
this->ArcherShop();
}
}
void BowShop::getBowShop()
{
//Display Choices
for (map<int, string>::iterator iter = WeaponArcher.begin(); iter != WeaponArcher.end(); iter++)
{
cout << iter->second << endl;
cout << endl;
}
}
void BowShop::setWeaponArcher(int i, string s)
{
WeaponArcher[i] = s;
}
//Inventory.h
#Pragma once
#include "MainShop.h"
class Inventory: public MainShop//access base class data like protected members
{
private:
int choice;
public:
void DisplayInventory();
void DisplayStats();
};
//Inventory.cpp
#include "Inventory.h"
void Inventory::DisplayInventory()
{
cout << "\nWhat do you want to do?\n1) Check Status\n2) Equip Weapons";//Equip what is in your inventory
cin >> choice;
switch (choice)
{
case 1: this->DisplayStats();
break;
case 2:cout << WeaponInventory.size() << endl;//debug
if (!WeaponInventory.empty())//Make sure inventory is not empty
{
cout << "Your current Weapons are: \n";
for (unsigned int i = 0; i < WeaponInventory.size(); ++i)
cout << i << ") " << WeaponInventory[i] << endl;
}
else cout << "You do not currently have any items!";
default: cout << "Error on switch!";
}
}
每次我运行它时,它会直接进入else
并显示我没有任何物品。我想为我的弓和剑店提供一个矢量因为矢量占用了大量的记忆而我的老师说要将它们保持在最低限度。所以我只想要一个从我的弓类和我的剑类中获取物品的矢量,但由于某种原因,它表现得好像我有多个矢量,每个矢量都有自己的一组物品。请帮忙!
答案 0 :(得分:2)
听起来你想要声明一个静态变量,所以所有子类实例都有相同的值。
您的问题是关于数据,您只能从班级内访问受保护的成员,不允许从班级外触摸“受保护”成员。您需要将受保护的变量更改为公共或添加公共方法来执行您要完成的任务。