Domanda

Ok, so I have a 3 level inheritance tree of vehicles. The leaf classes (the last ones down the tree) are six. I want to make a function that will read from a .txt file, and then assign the information to different variables (e.g. the file will contain 2006 Toyota Supra Diesel and these will be assigned to year, brand, model, fuel_type). Actually I managed to do this, but afterwards when I split my code into .h and .cpp files the function stopped working.

void Cars::txt_input()
{
    getline(file,input);          //This is in the .cpp file for one of the 
    year = atoi(input.c_str());   //child classes.
    getline(file,input);
    brand = input;
    getline(file,input);
    model = input;
    getline(file,input);
    passenger_capacity = atoi(input.c_str());
    getline(file,input);
    engine_power = atoi(input.c_str());
    getline(file,input);
    max_speed = atoi(input.c_str());    
    getline(file,input);
    fuel_type = input;
    getline(file,input);
    wheel_drive = input;
    getline(file,input);
    average_cons = atoi(input.c_str());
    getline(file,input);
    number_doors = atoi(input.c_str());
    getline(file,input);
    cType = input;
    getline(file,input);
    price = atoi(input.c_str());
}

In my main() I have declared ifstream file("vehicles.txt") and string input, which worked fine before splitting the code. Also,I have created the ifstream file and string input variables as protected members of the base class, otherwise it won't compile because it doesn't find any file and input variables to work with. However, now it seems the function does not take the variables from main() at all, and just fills all the objects' fields with 0 or N/A (according to the constructor.)

for (int i = 0; i < 11; i++)
{
    if(i<4) 
        vehicles.push_back(new Cars());
        vehicles[vehicles.size()-1]->txt_input();
    }
    if(i==4 || i==5)
    {
        vehicles.push_back(new Bus());
        vehicles[vehicles.size()-1]->txt_input();
    }
    //...
}

This is how I create the objects in my main(). As I said, when I had all the code in my Main.cpp it worked perfectly. Also,I tried passing (ifstream file,string input) as arguments to the txt_input() function but it produces an error C2248. This is my first-time post,sorry if there is any ambiguity. I'll provide more code/information if needed.

class Vehicles
{
public:

    Vehicles();

    virtual void txt_input();
    virtual void user_input();
    virtual void output();

    void red_output();

    int getPC();
    int getPrice();
    string getBrand();

    void open_file();

protected:
    string brand, model, type;
    int year, passenger_capacity, price;
    double engine_power, max_speed;
    ifstream file;
    string input;
};

This is the parent class.

È stato utile?

Soluzione

So, the problem, according to comments, is that your variable file is now a member of Vehicles unrelated with the previous ifstream in your main...

So the following may help:

/*virtual*/ void Vehicles::txt_input(std::istream& file)
{
    std::string input;    

    getline(file, input);
    year = atoi(input.c_str());
    getline(file, input);
    brand = input;
    // And so on... 

}

And so your loop becomes :

std::ifstream file("vehicles.txt");
for (int i = 0; i != 4; i++) {
    Cars* cars = new Cars();
    cars->txt_input(file);
    vehicles.push_back(cars);
}
for (int i = 0; i != 2; i++) {
    Bus* bus = new Bus();
    bus->txt_input(file);
    vehicles.push_back(bus);
}

Altri suggerimenti

You have set variable file in main but using the member variable also named file which is not being set to vehicles.txt. One way is to make the constructor take the file name as a parameter.

E.g. code - not compiled Constructor

Cars::Cars(const ifstream& if) : file(if) { }

When adding it to the vector vehicles.push_back(new Cars(file)); // this is the file defined in main set to "vehicles.txt"

Note: Use conventions when naming your class member variables (e.g. mFile, m_file, _file) to avoid confusion.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top