2013-02-22 67 views
0

給出以下代碼段1和2中的兩個類定義,能否告訴我爲什麼我得到第三個代碼段中顯示的編譯錯誤,以及如何修復他們?它看起來子類無法訪問基類中的非虛擬公共方法(請參閱第二代碼片段中標記爲第197行的子類中的getVipAddress方法)。我是C++領域的新手,所以我很感激你是否碰巧看到代碼塊中需要的其他改進。從子類無法訪問C++公共非虛擬基類方法

SNIPPET 1(Base和亞類)

//基類

class BaseEntity 
{ 
public: 
BaseEntity(): mUUID(""), mName("") {} 
BaseEntity(std::string &uuid, std::string &name): mUUID(uuid), mName(name) { } 

BaseEntity (const BaseEntity &copyin) 
{ 
    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 &copyin) 
    { 
      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’ 

回答

3

類的默認繼承是私有的。你的聲明更改爲

class VipAddressSet : public BaseEntity 
1

那是因爲你必須用public修飾符繼承了

class VipAddressSet : public BaseEntity 
1

我猜你是用private繼承。嘗試

class VipAddressSet : public BaseEntity 

否則該方法將不可見。