在下面的代码段1和代码段2中给出了两个类定义,您能告诉我为什么我收到第3个代码段中显示的编译错误,我该如何解决它们?它看起来子类无法访问基类中的非虚拟公共方法(请参阅第二个代码片段中标记为第197行的子类中的方法getVipAddress)。我是C ++领域的新手,所以如果您碰巧看到代码块中需要任何其他改进,我将不胜感激。
SNIPPET 1(基础和子类)
// BASE CLASS
class BaseEntity
{
public:
BaseEntity(): mUUID(""), mName("") {}
BaseEntity(std::string &uuid, std::string &name): mUUID(uuid), mName(name) { }
BaseEntity (const BaseEntity ©in)
{
this->mUUID = copyin.mUUID.c_str();
this->mName = copyin.mName.c_str();
}
BaseEntity operator = (const BaseEntity &rhs)
{
this->mUUID = rhs.mUUID.c_str();
this->mName = rhs.mName.c_str();
return *this;
}
~BaseEntity() { };
void setUUID(std::string uuid) { mUUID = uuid; }
void setName(std::string name) { mName = name; }
/*LINE 89 is the following*/
const std::string &getUUID() const { return mUUID; }
const std::string &getName() const { return mName; }
private:
std::string mUUID;
std::string mName;
};
// SUBCLASS
class VipAddressSet : BaseEntity
{
public:
VipAddressSet() : BaseEntity() { }
VipAddressSet(std::string &uuid, std::string &name) : BaseEntity(uuid, name) { }
VipAddressSet(const VipAddressSet ©in)
{
setUUID(copyin.getUUID());
setName(copyin.getName());
std::vector<VipAddressEntity>::const_iterator iter;
for( iter = copyin.mVipAddressList.begin(); iter != copyin.mVipAddressList.end(); iter++ )
{
addVipAddress(*iter);
}
}
VipAddressSet operator = (const VipAddressSet &rhs)
{
setUUID(rhs.getUUID());
setName(rhs.getName());
std::vector<VipAddressEntity>::const_iterator iter;
for( iter = rhs.mVipAddressList.begin(); iter != rhs.mVipAddressList.end(); iter++ )
{
addVipAddress(*iter);
}
return *this;
}
int getVipAddress(std::string &uuid, VipAddressEntity **ptr )
{
std::vector<VipAddressEntity>::const_iterator iter;
for( iter = mVipAddressList.begin(); iter != mVipAddressList.end(); iter++ )
{
**/*~~~~ LINE 197 is the following ~~~~*/**
if(iter->getUUID() == uuid)
{
**ptr = *iter;
return 1;
}
}
return 0;
}
const std::vector<VipAddressEntity>& getVipAddressList() const { return mVipAddressList; }
private:
std::vector<VipAddressEntity> mVipAddressList;
void addVipAddress(const VipAddressEntity &entity)
{
mVipAddressList.push_back(entity);
}
};
汇编输出
$ g++ -g -c -std=c++11 -Wall Entity.hpp
Entity.hpp: In member function ‘int ECLBCP::VipAddressSet::getVipAddress(std::string&, ECLBCP::VipAddressEntity**)’:
Entity.hpp:89:21: error: ‘const string& ECLBCP::BaseEntity::getUUID() const’ is inaccessible
Entity.hpp:197:24: error: within this context
Entity.hpp:197:24: error: ‘ECLBCP::BaseEntity’ is not an accessible base of ‘const ECLBCP::VipAddressEntity’
答案 0 :(得分:3)
类的默认继承是私有的。将您的声明更改为
class VipAddressSet : public BaseEntity
答案 1 :(得分:1)
那是因为你必须使用public modifier
继承它class VipAddressSet : public BaseEntity
答案 2 :(得分:1)
我猜你是继承private
。尝试
class VipAddressSet : public BaseEntity
否则该方法将不可见。