访问基类C 中的变量
Accessing variables in base class C++
嗨,这是我的基类游侠的标头文件,在其中我有保护变量fov_,usb_ ...我希望通过我的getter函数访问,我有三个孩子上课的课程。
ranger.h
#ifndef RANGER_H
#define RANGER_H
using namespace std;
class Ranger
{
//private contructor prevents contruction of base class
Ranger();
public:
void setBaud(int baud);
virtual void setFOV(int fov) = 0;
void setSamp(int sam);
int getFOV();
int getBaud();
int getMaxRange();
int getUSB();
protected:
//protected variables that are each indivdualy owned by each sensor
int fov_;
int maxRange_;
int usb_;
int baud_;
int samp_;
double data[];
//protected contructors for the child classes to use to set fixed parameters
Ranger(int fov, int maxRange, int port);
Ranger(int maxRange, int port);
};
#endif // RANGER_H
这是我的CPP文件,用于包含Getter文件的基类,它只有已验证的变量的返回。
Ranger::Ranger()
{
}
Ranger::Ranger(int fov, int maxRange, int port)
{
fov_ = fov;
maxRange_ = maxRange;
usb_ = port;
}
Ranger::Ranger(int maxRange, int port)
{
maxRange_ = maxRange;
usb_ = port;
}
void Ranger::setBaud(int baud)
{
switch(baud)
{
case 0: baud_ = 38400; break;
case 1: baud_ = 115200; break;
default: baud_ = 38400; break;
}
}
void Ranger::setSamp(int sam)
{
samp_ = sam;
}
int Ranger::getFOV()
{
return fov_;
}
int Ranger::getBaud()
{
return baud_;
}
int Ranger::getMaxRange()
{
return maxRange_;
}
int Ranger::getUSB()
{
return usb_;
}
,在我的主体中,我想从基类访问受保护的变量,以防止重新编写代码,因此每个Childs变量在基类中都受到保护。我尝试通过las.getfov((访问这些访问,但是我会收到一个细分故障错误,这意味着我无法访问它们,而且我不太了解为什么。
main.cpp
int main( int argc, char ** argv)
{
Laser las;
int baud;
cout << "Baud:" << endl;
cout << "0 - 38400" << endl;
cout << "1 - 115200" << endl;
cin >> baud;
las.setBaud(baud);
cout << "Baud for Lazer sensor is "+las.getBaud() << endl;
cout << "Lazer sensor created..." << endl;
cout << "Lazer's FOV: " + las.getFOV() << endl;
cout << "Lazer's Max Range: " + las.getMaxRange() << endl;
cout << "Lazer's Port: " + las.getUSB() << endl;
Radar rad;
int baud2;
cout << "Baud:" << endl;
cout << "0 - 38400" << endl;
cout << "1 - 115200" << endl;
cin >> baud2;
rad.setBaud(baud2);
cout << "Baud for Radar sensor is "+rad.getFOV() << endl;
int fov;
cout << "Feild of View Of Radar:" << endl;
cout << "0 - 20 degrees" << endl;
cout << "1 - 40 degrees" << endl;
cin >> fov;
rad.setFOV(fov);
cout << "FOV is set to " + rad.getFOV() << endl;
cout << "Radar sensor created..." << endl;
cout << "Radar's FOV: ' " + rad.getFOV() << endl;
cout << "Radar's Max Range: " + rad.getMaxRange() << endl;
cout << "Radar's Port: " + rad.getUSB() << endl;
Sonar son;
//rad.setFOV(user);
}
这是儿童课程的CPP文件之一(lazer(laser.cpp
#include "laser.h"
Laser::Laser() : Ranger(180,8,0)
{
};
void Laser::setFOV(int fov)
{
fov_ = fov;
}
laser.h
#ifndef LASER_H
#define LASER_H
#include "ranger.h"
#include "rng.h"
class Laser : public Ranger
{
public:
Laser();
void setFOV(int fov);
};
#endif // LASER_H
谢谢所有发表评论的人,我知道我提出了太多代码以帮助你们,对不起,下次我会知道的,谢谢让我知道错误,我做了更多的研究,发现问题是当我打印出来时,您无法使用以下操作员:
cout<<""+function()<<endl;
相反,您需要像这样将功能与数组分开:
cout<<""<<function()<<endl;
谢谢大家。
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- 模板基类中的静态变量
- 在派生类中使用基类的私有成员变量的最佳方法
- 当空基类也是成员变量时,为什么禁止空基优化?
- 引用基类模板的成员变量的简单方法
- 如何访问基类向量中的子类变量?(对于实体组件系统)
- C++:使用基类中的仅派生类变量
- Qml 未收到基类成员变量的更新值
- 为什么继承的函数会更改基类中的成员变量,而不是与名为它的对象同名的对象
- 如何根据基类对象中的变量值分配派生类中的变量?
- 我如何实现从其基类获取变量的构造函数
- OOP - 抽象类类型,初始化基类和派生类中的变量
- 如何从基类向量达到派生的类变量
- 基类无法访问的公共成员变量
- 如何处理参数中的基类和指针变量
- 访问基类C 中的变量
- 如何将指针存储在实例变量中,该指针被声明为指向基类的指针
- 隐藏的成员变量不应在仅允许const访问的基类中突变,以便保留分配运算符
- 如何从派生类中的基类访问变量的值?
- 如何使用基类对象设置派生类变量的值