继承变量不可访问
Inheritance variables not accessible
我想从继承的类中访问变量。但是,编译器给出错误:furniture.cpp:55:9:错误:"int stool::n_StoolLegs"在此上下文中是私有的one.n_StoolLegs -1;。
这是一个家庭作业,但是,作业说我需要使用4个类型的粪便变量。所以我继承了这门课。是否有其他选项可以修改以修改继承的变量?
家具 .hpp
#include <ostream>
#include <string>
#ifndef FURNIURE_HPP
#define FURNITURE_HPP
class stool{
private:
int n_StoolLegs;
int n_seats;
public:
void setStoolLegs(int);
int getStoolLegs();
void setSeats(int);
int getSeats();
};
class table {};//not relevant
class furniture: public stool, public table
{
private:
stool one;
stool two;
stool three;
stool four;
table first;
public:
furniture(){
one.setStoolLegs(4);
one.setSeats(1);
void makeMoreHipster();
};
#endif
家具.ccp:
#include <iostream>
#include "furniture.hpp"
//begin stool
void stool::setStoolLegs (int nLegs){
n_StoolLegs = nLegs;
};
int stool::getStoolLegs(){
return n_StoolLegs;
};
void stool::setSeats (int nSeats){
n_seats = nSeats;
};
int stool::getSeats(){
return n_seats;
};
//end stool//
// begin table
//not relevant
// end table
//start furniture
void furniture::makeMoreHipster(){
one.n_StoolLegs -1;
};
// end furniture//
使用继承,private
变量不可访问。使用 protected
.
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 通过按地址访问变量
- 通过 get-Method 访问变量在类外不起作用
- 是否可以使用指针访问变量以避免直接编辑变量?
- 我无法访问变量的值,即使我通过范围传递了它
- 简单的问题 - 如何从单独的"结构"内的'class'访问变量?
- (C++) 从另一个类访问变量时值无效
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 是否可以从字符串访问变量?
- 变量 z 无法从其他函数访问变量 x
- 如何在 C++ 中从 void 返回函数访问变量
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 在C 中本地声明隐藏的封闭范围(而非全局)中访问变量
- 从函数中删除动态内存分配,但无法访问变量
- 如何从派生类中的基类访问变量的值?
- 如何通过指针更快地访问变量,然后直接访问变量?
- 如何在公共类 c++ 中访问变量的值
- 从 Visual Studio 中的多个源文件访问变量
- 使用命名空间和模板类访问变量
- 从子对话框访问变量