Vladimir Marenus
Vladimir Marenus

Reputation: 403

How Can I Access The Private Member Of A Class From Within a Header File?

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.

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.

I would like bag.h (more specifically, the addItem function) to be able to access the m_itemName variable of the created item.

aItem.cpp:

//aItem .cpp implementation file
#include "aItem.h"
#include <iostream>
#include <string>
#include <vector>
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;
}

aItem.h:

#ifndef AITEM_H_INCLUDED
#define AITEM_H_INCLUDED

#include <iostream>
#include <string>
#include <vector>
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

bag.h:

#ifndef BAG_H_INCLUDED
#define BAG_H_INCLUDED

#include "aItem.h"
#include <iostream>
#include <string>
#include <vector>
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<aItem>::size_type index = 0; index < m_items.size(); index++ )
            {
                //Makes a numerical list.
                cout << "Item " << index + 1 << ": " <<  m_items[index].m_itemName << endl;
                index+= 1;
            }
    }
private:
     vector<aItem> m_items;
};

#endif // BAG_H_INCLUDED

...and finally, main.cpp:

// WHYGODWHY.cpp : Defines the entry point for the console application.
//

#include "aItem.h"
#include "bag.h"
#include <iostream>
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 << "Name: " << woodSword.getName() << endl;
    cout << "Type: " << woodSword.getType() << endl;
    cout << "Damage: " << woodSword.getDamage() << endl;
    //cout << "Inv: " <<
    int pause = 0;
    cout << "Pause!";
    cin >> pause;

    return 0;
}

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 ===|

Upvotes: 2

Views: 2357

Answers (3)

Since you already have public accessors for the name, why not use them?

cout << "Item " << index + 1 << ": " <<  m_items[index].getName() << endl;

Consider changing the accessors to be const and return a const reference:

const std::string& getName() const;

Aside from that there are other issues. In particular, containers in C++ don't hold hold polymorphic objects, so a std::vector<aItem> can only hold aItem (but not bag objects). On a completely different level, rethink your naming conventions, it makes little sense to have a type (which is a category for different objects) name as aItem which means one entity of Item. This is also inconsistent with the name bag...

Upvotes: 2

Benoit Thiery
Benoit Thiery

Reputation: 6387

I would suggest to call getName() method instead of accessing directly members of the class. Also, why is bag inheriting from aItem in this case? If I understand correctly what you are trying to do, a bag is not a aItem so it should not inherit from aItem and should use access methods and not direct member access. As an alternative, you can declare bag as a friend of aItem as suggested by WhozCraig.

Upvotes: 1

Daniel Gehriger
Daniel Gehriger

Reputation: 7468

Use your class' public interface. So instead of using

cout << "Item " << index + 1 << ": " <<  m_items[index].m_itemName << endl;

do

cout << "Item " << index + 1 << ": " <<  m_items[index].getName() << endl;

Upvotes: 1

Related Questions