2017-04-27 63 views
0

嗨,這是我的基類Ranger的頭文件,並且在其中我保護了變量fov_,usb_ ...我想用我的getter函數訪問變量,我有三個子類這個。在基類C++中訪問變量

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文件,它只是有portected變量的迴歸。

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文件以供參考(拉澤) 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

+1

你的獲得者沒有任何問題。您問題中顯示的代碼不符合[mcve]的要求;因此沒有權威的答案是可能的,但是你崩潰的可能原因是你對'double data []'類成員是什麼的基本誤解,並且你期望它是事實以外的東西。查看您的C++書籍,以獲取有關指針和數組的更多信息。 –

+1

'我嘗試通過las.getFOV()訪問這些,但是我得到了分段錯誤錯誤,這意味着我無法訪問它們 - - 錯誤號。如果你沒有訪問權限,你會有編譯時錯誤,而不是seg錯誤。 – John3136

回答

0

謝謝大家誰評論說,我明白我把太多的代碼,以幫助你們了,很抱歉,我就知道下一次,和三江源到讓我知道了錯誤的區別,我已經做更多的研究,發現問題是當我打印出來,你不能使用運營商如:

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

相反,你需要separa從陣列功能如下:

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

謝謝你們。