Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类继承和成员函数_C++_Oop_Inheritance - Fatal编程技术网

C++ C++;类继承和成员函数

C++ C++;类继承和成员函数,c++,oop,inheritance,C++,Oop,Inheritance,全部,, 我很难理解为什么会出现以下错误。我保证这不是一个家庭作业问题,但是我对C++是新的!这是我的密码: #include <iostream> #include <string> #include <vector> class Base { protected: std::string label; public: Base(std::string _label) : label(_label) { } std::string get_la

全部,, 我很难理解为什么会出现以下错误。我保证这不是一个家庭作业问题,但是我对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;
}
#包括
#包括
#包括
阶级基础{
受保护的:
字符串标签;
公众:
Base(std::string _-label):label(_-label){}
std::string get_label(){return label;}
};
派生类:私有基{
私人:
std::字符串fancylabel;
公众:
派生(std::string\u标签,std::string\u fancylabel)
:Base{u label},fancylabel{{u fancylabel}{}
std::string get_fancylabel(){return fancylabel;}
};
类向量派生{
私人:
std::向量向量向量;
公众:
向量导出(int n)
{
vec_派生={};
对于(int i=0;istd::coutVisual Studio会发出一条错误消息,为您提供更多提示:

错误C2247:'Base::get_label'不可访问,因为'Derived'使用'private'从'Base'继承

因此,如果要通过
派生的
对象访问
Base::get_label
,则需要将基类公开:

class Derived : public Base
或将
get_标签
公开:

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

你为什么选择私有继承?你希望继承的东西是公开的吗?>有没有办法解决这个问题?是的,公开继承关于家庭作业的问题是,它们通常包含的信息太少,要求他人编写代码而不付出任何努力,诸如此类。询问家庭作业什么都不是dse@idclev463035818谢谢,修好了:)。好的,我想我明白了。谢谢!
class Derived : private Base {
public:
    using Base::get_label;