【问题标题】:Accessing variables in base class C++访问基类 C++ 中的变量
【发布时间】:2017-04-27 01:21:24
【问题描述】:

您好,这是我的基类 Ranger 的头文件,其中我有受保护的变量 fov_、usb_ ... 我希望使用我的 getter 函数访问这些变量,我在这个上有三个子类。

游侠.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

这是包含 getter 文件的基类的我的 cpp 文件,它只是返回了移植的变量。

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_;
}

在我的主类中,我想从基类访问受保护的变量以防止重写代码,因此每个子变量都在基类中受到保护。我尝试通过 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) 激光.cpp

#include "laser.h"

Laser::Laser() : Ranger(180,8,0)
{
};

void Laser::setFOV(int fov)
{
    fov_ = fov;
}

激光.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

【问题讨论】:

  • 你的吸气剂没有问题。您问题中显示的代码不符合minimal reproducible example 的要求;因此,没有权威的答案是可能的,但您崩溃的可能原因是您对 double data[] 类成员的基本误解,并且您期望它与实际不同。查看您的 C++ 书籍,了解有关指针和数组的更多信息。
  • I try to access these by las.getFOV() but I get a segmentation fault error meaning I don't have access to them - 错误号。如果您没有访问权限,您将遇到编译时错误而不是段错误。

标签: c++ inheritance segmentation-fault getter


【解决方案1】:

感谢所有评论的人,我知道我写了太多代码来帮助你们,对不起,我下次知道,谢谢你让我知道错误之间的区别,我做了更多研究并发现问题是当我将其打印出来时,您不能使用以下运算符:

cout<<""+function()<<endl;

相反,您需要像这样将函数与数组分开:

cout<<""<<function()<<endl;

谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多