访问基类C 中的变量

Accessing variables in base class C++

本文关键字:变量 基类 访问      更新时间:2023-10-16

嗨,这是我的基类游侠的标头文件,在其中我有保护变量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;

谢谢大家。