2

鉴于下面片段 1 和 2 中的两个类定义,您能否告诉我为什么会出现第三个片段中显示的编译错误以及如何修复它们?看起来子类无法访问基类中的非虚拟公共方法(请参阅第二个代码片段中标记为第 197 行的子类中的 getVipAddress 方法)。我是 C++ 领域的新手,所以如果您碰巧看到代码块中需要任何其他改进,我将不胜感激。

SNIPPET 1(基类和子类)

//基类

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;

};

//子类

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’
4

3 回答 3

5

类的默认继承是私有的。将您的声明更改为

class VipAddressSet : public BaseEntity
于 2013-02-22T12:45:00.593 回答
1

那是因为你必须用 public 修饰符继承它

class VipAddressSet : public BaseEntity
于 2013-02-22T12:45:36.633 回答
1

我猜你正在继承private. 尝试

 class VipAddressSet : public BaseEntity

否则该方法将不可见。

于 2013-02-22T12:45:53.137 回答