2016-09-12 13 views
-2

두 개의 헤더 파일 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 헤더를 변경하지 않고 어떻게 올바르게 수행 할 수 있습니까? 어떤 도움이라도 대단히 감사하겠습니다.

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(); 
      ^

전체 코드는 아래에 포함되어 있습니다 : g로 컴파일 할 때

이 오류는 ++받은 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()); 
} 

car.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 
+3

자체 포함 된 실행 가능 예제를 제공하지 않고 들여 쓰기가 어려운 코드를 사용하여 스키밍이 어려운 문제에 대해 상당히 약한 설명이 포함 된 과제 기반 질문을 게시한다는 점에 유의하십시오. 질문을 수정하지 않으면 잘되지 않을 것입니다. 최선의 충고는 문제를 당신이 이해하지 못하거나 혼란스럽게하는 부분을 정확히 잡아내는 자체 시험으로 분해하는 것입니다. 당신의 설명에 기초하여 당신은 자신이 무엇을 요구하는지 알지 못하는 것처럼 보입니다. – Dmitry

+0

load_car()가 호출되는 car 클래스의 구현 (즉 car.cpp 내부)에 어떤 위치가 있습니까? 일반적으로 누군가가 메서드를 비공개로 만들 때 그들은 같은 클래스의 외부에있는 누군가가 그 메서드를 호출하기를 원하지 않기 때문에 그렇게합니다. 따라서 car.h의 작성자는 car.cpp를 편집 할 수 있다면 load :: car() (car :: something() 메서드의 구현을 제외하고)를 호출하지 말 것을 제안합니다 –

+0

아마도 http://www.cplusplus.com/doc/tutorial/inheritance/에서'friend class'를 사용하십시오. 그러나 @Dmitry가 지적했듯이, 먼저 시도하십시오. – TuanDT

답변

2

당신이 만약 car.h를 수정할 수 없으면에서 a.load_car()으로 전화 할 수 없습니다..

main을 수정할 수없는 경우 잘못된 할당이 적용됩니다.

main을 직접 만드는 경우 a.load_car()으로 전화하지 않고 과제를 수행 할 수있는 방법을 찾아보십시오.

0

어둠과 마찬가지로 load_car() 함수는 생성자를 통해 호출되므로 main() 함수에서 호출되지 않아야합니다.