Witam pisze program na zajęcia, w którym na podstawie pliku tekstowego (coś w rodzaju bazy danych) chcę utworzyć zawartość kontenera. Znaczy kontener vector zawiera wskaźniki na obiekty klasy, a te obiekty tworzę z pliku tekstowego. Oczywiście chcę też móc dopisywać do pliku i kasować z pliku. Może kod lepiej zobrazuje o co mi chodzi:
main:
#include "bureau.h"
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
Bureau *bureau = new Bureau();
bureau->load_cars_from_txt();
bureau->show_cars();
delete bureau;
return 0;
}
Vehicle.h :
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
class Vehicle
{
friend class Bureau;
private :
static int id;
int year;
std::string marque, model;
double price;
public:
Vehicle();
Vehicle(int, std::string, std::string, int, double);
~Vehicle();
std::string to_string();
};
#endif // VEHICLE_H
Vehicle.cpp :
#include "vehicle.h"
#include <iostream>
#include <sstream>
int Vehicle::id = 1;
Vehicle::Vehicle()
{
}
Vehicle::Vehicle(int id, std::string marque, std::string model, int year, double price)
{
this->id = id;
this->marque = marque;
this->model = model;
this->year = year;
this->price = price;
}
Vehicle::~Vehicle()
{
}
std::string Vehicle::to_string()
{
std::stringstream ss;
std::string strings;
ss << id << " " << marque << " " << model << " " << year << " " << price;
strings = ss.str();
return strings;
}
Bureau.h :
#ifndef BUREAU_H
#define BUREAU_H
#include "vehicle.h"
#include <vector>
class Bureau
{
private:
std::vector <Vehicle*> cars;
Vehicle *car;
public:
Bureau();
~Bureau();
void load_cars_from_txt();
void add_car();
void show_cars();
friend class Vehicle;
};
#endif // BUREAU_H
Bureau.cpp :
#include "bureau.h"
#include "vehicle.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
//std::vector <Vehicle*> cars;
std::ofstream out;
std::ifstream in;
Bureau::Bureau()
{
//load_cars_from_txt();
}
Bureau::~Bureau()
{
cout << "Game Over!" << endl;
}
void Bureau::load_cars_from_txt()
{
in.open("data.txt", std::ios::in);
std::stringstream ss;
int id, year;
std::string marque, model, strings;
double price;
do
{
getline(in, strings);
ss << strings;
ss >> id >> marque >> model >> year >> price;
ss.clear();
//cout << numer << naz << endl;
car = new Vehicle(id, marque, model, year, price);
cars.push_back(car);
} while (!in.eof());
//samochody.erase(samochody.begin());
cars.pop_back();
in.close();
}
void Bureau::add_car()
{
int id, year;
std::string marque, model;
double price;
out.open("data.txt", std::ios::out | std::ios::app);
std::ostream & seekp(std::streamoff eof);
cout << "Podaj marke: ";
cin >> marque;
cout << "Podaj model: ";
cin >> model;
cout << "Podaj rok: ";
cin >> year;
cout << "Podaj cene: ";
cin >> price;
cout << endl;
id = cars.size() + 1;
out << id << " " << marque << " " << model << " " << year << " " << price;
//cout << nr << " " << naz << endl;
car = new Vehicle(id, marque, model, year, price);
cars.push_back(car);
std::ostream & seekp(std::streamoff eof);
out.close();
}
std::ostream &operator << (std::ostream &os, Vehicle &u) {
return os << u.to_string();
}
void show_cars()
{
for (size_t i = 0; i < cars.size(); i++)
{
cout << *(cars[i]) << endl;
}
}
Problem pojawia się w metodzie
show_cars
, kompilator wywala mi błąd, że nie ma cars
znaczy kontenera. Ale dlaczego?