Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I access a specific protected inherited member depending on the class?
    text
    copied!<p>Okay so I have <code>MainShop</code>(base class) then <code>SwordShop</code> and <code>BowShop</code>(derived classes) and another derived class called <code>Inventory</code>. I have a vector under protected in my base class, and when I access it from my derived classes it's okay, but each one has its own value. How can I set them all to have the same value?</p> <pre><code>//MainShop.h #pragma once class MainShop { private: //some variables protected: vector &lt;string&gt; 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-&gt;setWeaponSoldier(1, "1) Meito Ichimonji\n +4 Damage\n 150Gold"); this-&gt;setWeaponSoldier(2, "2) Shusui\n +10 Damage\n 230Gold"); this-&gt;setWeaponSoldier(3, "3) Elixir\n +16 Damage\n 300Gold"); this-&gt;setWeaponSoldier(4, "4) Blade of scars\n +24 Damage\n 550Gold"); this-&gt;setWeaponSoldier(5, "5) Ragnarok\n +32 Damage\n 610Gold"); this-&gt;setWeaponSoldier(6, "6) Eternal Darkness\n +40 Damage\n 690Gold"); this-&gt;setWeaponSoldier(7, "7) Masamune\n +52 Damage\n 750Gold"); this-&gt;setWeaponSoldier(8, "8) Soul Calibur\n +60 Damage\n 900Gold"); this-&gt;getSwordShop(); cout &lt;&lt; "What would you like to buy?"; cin &gt;&gt; choice; switch (choice) { case 1: WeaponInventory.push_back("Meito Ichimonji"); cout &lt;&lt; "You have Successfully Bought Meito Ichimonji\nIt has been added to your inventory\n"; break; case 2: WeaponInventory.push_back("Shusui"); cout &lt;&lt; "You have Successfully Bought Shusui\nIt has been added to your inventory\n"; break; //ETC default: cout &lt;&lt; "Error! You have entered an invalid answer\nPlease try again"; this-&gt;soldierShop(); } cout &lt;&lt; "your total items are: "&lt;&lt; WeaponInventory.size(); </code></pre> <p>Okay So lets say I bought two items. Here it would display that I have 2 items. But if I do <code>cout &lt;&lt; "your total items are: "&lt;&lt; WeaponInventory.size();</code> in my Inventory.cpp it would say I have 0! That's my problem.</p> <pre><code>void SwordShop::getSwordShop() { //Display Choices for (map&lt;int, string&gt;::iterator iter = WeaponSoldier.begin(); iter != WeaponSoldier.end(); iter++) { cout &lt;&lt; iter-&gt;second &lt;&lt; endl; cout &lt;&lt; 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-&gt;getBowShop();//Display options cout &lt;&lt; "What would you like to buy?"; cin &gt;&gt; choice2; switch (choice2) { case 1: WeaponInventory.push_back("Arondight"); cout &lt;&lt; "You have Successfully Bought Arondight\nIt has been added to your inventory\n"; break; case 2: WeaponInventory.push_back(" Gugnir"); cout &lt;&lt; "You have Successfully Bought Gugnir\nIt has been added to your inventory\n"; break; //ETC default: cout &lt;&lt; "Error! You have entered an invalid answer\nPlease try again"; this-&gt;ArcherShop(); } } void BowShop::getBowShop() { //Display Choices for (map&lt;int, string&gt;::iterator iter = WeaponArcher.begin(); iter != WeaponArcher.end(); iter++) { cout &lt;&lt; iter-&gt;second &lt;&lt; endl; cout &lt;&lt; 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 &lt;&lt; "\nWhat do you want to do?\n1) Check Status\n2) Equip Weapons";//Equip what is in your inventory cin &gt;&gt; choice; switch (choice) { case 1: this-&gt;DisplayStats(); break; case 2:cout &lt;&lt; WeaponInventory.size() &lt;&lt; endl;//debug if (!WeaponInventory.empty())//Make sure inventory is not empty { cout &lt;&lt; "Your current Weapons are: \n"; for (unsigned int i = 0; i &lt; WeaponInventory.size(); ++i) cout &lt;&lt; i &lt;&lt; ") " &lt;&lt; WeaponInventory[i] &lt;&lt; endl; } else cout &lt;&lt; "You do not currently have any items!"; default: cout &lt;&lt; "Error on switch!"; } } </code></pre> <p>Here every time I run it, it goes straight to the <code>else</code> and it displays that I do not have any items. I want to just have one vector for both my bow and Sword shops because vectors take up a lot of memory and my teacher said keep them to a minimum. So I just want one vector that takes in items from my bow class as well as my sword class, but for some reason it's acting as if I have multiple vectors, each with its own set of items. Please help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload