简体   繁体   English

C++ class inheritance 和成员函数

[英]C++ class inheritance and member functions

All, I am having difficulty understanding why I am getting the following error.所有,我很难理解为什么会出现以下错误。 I promise this is not a homework problem, but I am new to C++: Here is my code:我 promise 这不是作业问题,但我是 C++ 的新手:这是我的代码:

#include <iostream>
#include <string>
#include <vector>

class Base {
protected:
  std::string label;
public:
  Base(std::string _label) : label(_label) { }
  std::string get_label() { return label; }
};

class Derived : private Base {
private:
  std::string fancylabel;
public:
  Derived(std::string _label, std::string _fancylabel)
   : Base{_label}, fancylabel{_fancylabel} { }
  std::string get_fancylabel() { return fancylabel; }
};

class VecDerived {
private:
  std::vector<Derived> vec_derived;
public:
  VecDerived(int n)
  {
    vec_derived = {};
    for (int i = 0; i < n; ++i) {
      Derived newDerived(std::to_string(i), std::to_string(2*i));
      vec_derived.push_back(newDerived);
    }
  }
  std::string get_label(int n)  { return vec_derived.at(n).get_label(); }
  std::string get_fancylabel(int n) { return vec_derived.at(n).get_fancylabel(); }
};

int main (void)
{
  VecDerived obj(5);
  std::cout << obj.get_label(2) << " " << obj.get_fancylabel(2) << "\n";
  return 0;
}

The compiler error I get is as follows:我得到的编译器错误如下:

test1.cpp: In member function ‘std::__cxx11::string VecDerived::get_label(int)’:
test1.cpp:33:70: error: ‘std::__cxx11::string Base::get_label()’ is inaccessible within this context
   std::string get_label(int n)  { return vec_derived.at(n).get_label(); }
                                                                  ^
test1.cpp:9:15: note: declared here
   std::string get_label() { return label; }
               ^~~~~~~~~
test1.cpp:33:70: error: ‘Base’ is not an accessible base of ‘__gnu_cxx::__alloc_traits<std::allocator<Derived> >::value_type {aka Derived}’
   std::string get_label(int n)  { return vec_derived.at(n).get_label(); }

I don't understand why the member function get_label() wasn't inherited from the Base class to the Derived class, such that I'm not able to access it via the VecDerived class. I don't understand why the member function get_label() wasn't inherited from the Base class to the Derived class, such that I'm not able to access it via the VecDerived class. Is there a way that this can be resolved?有没有办法可以解决这个问题? Thanks in advance for your help!在此先感谢您的帮助!

Visual Studio emits an error message that gives you more hints: Visual Studio 发出一条错误消息,为您提供更多提示:

error C2247: 'Base::get_label' not accessible because 'Derived' uses 'private' to inherit from 'Base'错误 C2247:“Base::get_label”不可访问,因为“Derived”使用“private”从“Base”继承

So if you want to access Base::get_label through a Derived object, then you either need to make the base class public:因此,如果您想通过Derived的 object 访问Base::get_label ,那么您需要将基础 class 公开:

class Derived : public Base

or make get_label public:或公开get_label

class Derived : private Base {
public:
    using Base::get_label;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM