【问题标题】:Trouble accessing an object's private function无法访问对象的私有函数
【发布时间】:2017-01-19 10:18:32
【问题描述】:

我有一个作业,其中我无法编辑两个头文件 car.h 和 compass.h。在文件 car.h 中,有一个名为的私有函数: void load_car();。稍后在 car.cpp 中将其定义为:

void car::load_car(){
cout << "Please enter make and model:" << endl;
ifstream inFile;
string fileName;
fileName = (make + "-" + model + ".txt");
inFile.open(fileName.c_str());
//not a finished function
}

我的问题是我有一个主函数,

int main() {
cin >> make >> model;
car a(make, model);
a.load_car();

return 0;
}

这里我不能调用对象的私有成员函数。如何在不更改 car.h 标头的情况下正确执行此操作。任何帮助将不胜感激。

用g++编译时收到的错误是:

In file included from car.cpp:2:0:
car.h: In function ‘int main()’:
car.h:22:7: error: ‘void car::load_car()’ is private
  void load_car();
       ^
car.cpp:14:13: error: within this context
    a.load_car();
               ^

完整代码如下: 汽车.cpp

#include "compass.h"
#include "car.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string make, model;

int main() {
    cin >> make >> model;
    car a(make,model);
    a.load_car();


    return 0;
}

void car::load_car(){

    cout << "Please enter make and model:" << endl;
    ifstream inFile;
    string fileName;
    fileName = (make + "-" + model + ".txt");
    inFile.open(fileName.c_str());
}

汽车.h

#ifndef CAR_H
#define CAR_H

#include <string>
#include "compass.h"

//Relative direction
enum class rDir {left, right};

class car
{
private:
    std::string make, model;
    int topSpeed, horsepower, mass;
    double currentSpeed = 0;

//Defined by compass.h: an x/y coordinate struct and cardinal direction.
    coordinates position;
    compass direction;

//Helper functions
    void load_car();
    void update_position();
public:

//Constructor/Destructor
    car (std::string ma, std::string mo) : make (ma), model (mo) {load_car();}
    ~car() {};

//Getters
    std::string get_make() {return make;}
    std::string get_model() {return model;}
    coordinates get_position() {return position;}
    compass get_direction() {return direction;}
    double get_speed() {return currentSpeed;}

//Things cars do
    void accelerate();
    void brake();
    void coast();
    void steer (rDir turn);
};


#endif // CAR_H

【问题讨论】:

  • 请注意,您正在发布一个基于作业的问题,该问题的描述相当薄弱,难以浏览,代码不缩进,没有提供自包含的可运行示例。除非您修改您的问题,否则我不会顺利进行。我最好的建议是将您的问题分解为独立的测试,准确地捕获您不理解或使您感到困惑的部分。根据您的描述,您自己似乎并不知道自己在问什么。
  • 在汽车类的实现中(即在 car.cpp 中)是否有调用 load_car() 的地方?通常,当有人像这样将方法设为私有时,他们这样做是因为他们特别不希望同一类之外的任何人调用该方法。因此,这表明 car.h 的作者不希望您调用 load_car() (如果允许您编辑 car.cpp,则可能来自 car::something() 方法的实现)
  • 也许您可以使用文档中的friend classcplusplus.com/doc/tutorial/inheritance 但正如@Dmitry 指出的那样,请先尝试。
  • 在您的编辑显示代码之后 - 汽车构造函数已经调用了 load_car,所以您不需要。
  • @JasonC 是的,cout 不合适,因为我试图在编写更多函数之前测试代码。后来我意识到我遇到的问题是我试图调用辅助函数,因为我忽略了构造函数本身已经在调用该函数。很抱歉给您带来不便

标签: c++ function header-files private member


【解决方案1】:

如果你不允许修改car.h,你将无法从main调用a.load_car()

如果你不允许修改main,你有一个错误的分配。

如果您要创建自己的 main,请找到一种无需致电 a.load_car() 即可完成任务的方法。

【讨论】:

    【解决方案2】:

    正如 The Dark 提到的,load_car() 函数是通过构造函数调用的,因此不应该在 main() 函数中调用。

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 2020-10-10
      • 1970-01-01
      • 2018-08-18
      • 2015-10-12
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多