Given two class definitions in snippet 1 and 2 below, can you please tell me why I am getting the compilation errors shown in the 3rd snippet and how can I fix them? It looks the sub-class cannot access to the non-virtual public methods in base class (see the method getVipAddress in the subclass marked as line 197 in the 2nd code snippet). I am new to C++ realm, so I appreciate if you happen to see any other improvement needed in the code blocks.
SNIPPET 1 (Base and Sub-class)
//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);
}
};
COMPILATION OUTPUT
$ 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’
The default inheritance for a class is private. Change your declaration to
class VipAddressSet : public BaseEntity
那就是因为您必须使用public修饰符继承它
class VipAddressSet : public BaseEntity
I guess you are inheriting with private
. Try
class VipAddressSet : public BaseEntity
otherwise the method won't be visible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.