AWT
AWT

Reputation: 381

C++ cannot access element of vector of abstract class?

I have 2 simple C++ headers implemented as in the following:

Attribute.h

#include <string>
using namespace std;

class IAttribute
{
    virtual string getName(){};
};

class StringAttribute : public IAttribute
{
private:
    string name = "";
    string value = "";

public:
    StringAttribute(string name, string value)
    {
        this->name = name;
        this->value = value;
    }
    string getName()
    {
        return this->name;
    }
    string getStrValue()
    {
        return value;
    }
    void setValue(string value)
    {
        this->value = value;
    }
};

tableRow.h

#include "attribute.h"
#include <vector>
using namespace std;
class TableRow
{
private:
    vector<IAttribute *> attributeList;
    int rowId;

public:
    TableRow(int rowId)
    {
        this->rowId = rowId;
    }

    void addStrAttribute(string name, string value)
    {    
        attributeList.push_back(new StringAttribute(name, value));
    }
    StringAttribute getStrAtt(string name)
    {
        for (int i = 0; i < (int)attributeList.size(); i++)
        {
            if (attributeList[i]->)//couldn't access the methods of StringAttributeImp
            {

            }
        }
    }
};  

As in the comment of tableRow header above, I couldn't access the methods and properties of the Implementation class. What is wrong?

Upvotes: 0

Views: 216

Answers (2)

coo
coo

Reputation: 114

You should change the getName function to public; or use friend class.

Upvotes: 1

PaulMcKenzie
PaulMcKenzie

Reputation: 35454

The getName function is private in the IAttribute class. So of course you're not able to access it.

Upvotes: 2

Related Questions