Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Can I Access The Private Member Of A Class From Within a Header File?
    text
    copied!<p>I'm sorry that the question is worded so awkwardly, as I'm not sure how to ask it. In any case, I have an implementation file and header for a class called "aItem", which contains a vector of aItem objects.</p> <p>In a second file, entitled "bag", I'd like to be able to make a list of all the elements of the aforementioned vector. I originally had "bag" separated into a header and implementation file, but the compiler threw a fit about "undeclared reference to " until I combined the two files. In any case, that part is working. </p> <p>I would like bag.h (more specifically, the addItem function) to be able to access the m_itemName variable of the created item.</p> <p>aItem.cpp:</p> <pre><code>//aItem .cpp implementation file #include "aItem.h" #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; //setting this up default aItem::aItem() { m_itemName = "Default Name"; m_itemType = "Default Type"; m_damage = 9001; } void aItem::setName(string name) { m_itemName = name; } void aItem::setType(string type) { m_itemType = type; } void aItem::setDamage(int damage) { m_damage = damage; } string aItem::getName() { return m_itemName; } string aItem::getType() { return m_itemType; } int aItem::getDamage() { return m_damage; } </code></pre> <p>aItem.h:</p> <pre><code>#ifndef AITEM_H_INCLUDED #define AITEM_H_INCLUDED #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; class aItem { public: //constructor aItem(); //Methods void ItemCreate(string itemName, string itemType, int damage); void setName(string name); void setType(string type); void setDamage(int damage); string getName(); string getType(); int getDamage(); private: string m_itemName; string m_itemType; int m_damage; }; #endif // AITEM_H_INCLUDED </code></pre> <p>bag.h:</p> <pre><code>#ifndef BAG_H_INCLUDED #define BAG_H_INCLUDED #include "aItem.h" #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; class aItem; class bag : public aItem { public: //constructor bag(); //methods //void delItem(aItem aitem); void addItem(aItem var) { m_items.push_back(var); } void bagList() { for( vector&lt;aItem&gt;::size_type index = 0; index &lt; m_items.size(); index++ ) { //Makes a numerical list. cout &lt;&lt; "Item " &lt;&lt; index + 1 &lt;&lt; ": " &lt;&lt; m_items[index].m_itemName &lt;&lt; endl; index+= 1; } } private: vector&lt;aItem&gt; m_items; }; #endif // BAG_H_INCLUDED </code></pre> <p>...and finally, main.cpp:</p> <pre><code>// WHYGODWHY.cpp : Defines the entry point for the console application. // #include "aItem.h" #include "bag.h" #include &lt;iostream&gt; using namespace std; int main() { aItem woodSword; woodSword.setName("Wooden Sword"); woodSword.setDamage(3); woodSword.setType("WPN"); bag inventory; inventory.addItem(woodSword); inventory.bagList(); cout &lt;&lt; "Name: " &lt;&lt; woodSword.getName() &lt;&lt; endl; cout &lt;&lt; "Type: " &lt;&lt; woodSword.getType() &lt;&lt; endl; cout &lt;&lt; "Damage: " &lt;&lt; woodSword.getDamage() &lt;&lt; endl; //cout &lt;&lt; "Inv: " &lt;&lt; int pause = 0; cout &lt;&lt; "Pause!"; cin &gt;&gt; pause; return 0; } </code></pre> <p>EDIT: This is the error message: C:\Users\dmarr\Documents\CSharper\Dallas\CPlusPlus\WHYGODWHYCB\aItem.h||In member function 'void bag::bagList()':| C:\Users\dmarr\Documents\CSharper\Dallas\CPlusPlus\WHYGODWHYCB\aItem.h|28|error: 'std::string aItem::m_itemName' is private| C:\Users\dmarr\Documents\CSharper\Dallas\CPlusPlus\WHYGODWHYCB\bag.h|32|error: within this context| ||=== Build finished: 2 errors, 0 warnings ===|</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