I have a program that reads the content from a file and creates objects.
I store these objects into a vector<Component *>
. So I can call a virtual function on each object.
The virtual function does not work. Any other class function works fine on the vector objects.
Does anybody know what is going on?
void upload(vector<Component*>);
vector<Component*>download();
int main() {
vector<Component*>vec = download();
vec[0]->printSpecs(); // read acces violation
// Write all products from vector to file (overwrite)
}
void upload(vector<Component*> x) {
// write each class object to specific file
ofstream f;
for (int i = 0; i < x.size(); i++) {
if (x[i]->rank() == 1) {
f.open("CPU.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(CPU));
f.close();
}
if (x[i]->rank() == 2) {
f.open("Case.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(Case));
f.close();
}
if (x[i]->rank() == 3) {
f.open("PS.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(PS));
f.close();
}
if (x[i]->rank() == 4) {
f.open("GPU.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(GPU));
f.close();
}
if (x[i]->rank() == 5) {
f.open("HD.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(HD));
f.close();
}
if (x[i]->rank() == 6) {
f.open("RAM.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(RAM));
f.close();
}
if (x[i]->rank() == 7) {
f.open("SSD.txt", ios::binary);
f.write(reinterpret_cast<char*>(x[i]), sizeof(SSD));
f.close();
}
}
}
vector<Component*> download() {
vector<Component*>items;
ifstream f;
// read cpu objects into vector
f.open("CPU.txt", ios::binary);
while (!f.eof()) {
static CPU temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(CPU));
Component* x = &temp;
items.push_back(x);
}
items.pop_back();
f.close();
// read GPU objects into vector
f.open("GPU.txt", ios::binary);
while (!f.eof()) {
static GPU temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(GPU));
Component* x = &temp;
items.push_back(x);
}
items.pop_back();
f.close();
// read HD objects into vector
f.open("HD.txt", ios::binary);
while (!f.eof()) {
static HD temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(HD));
Component* x = &temp;
items.push_back(x);
}
items.pop_back();
f.close();
// read PS objects into vector
f.open("PS.txt", ios::binary);
while (!f.eof()) {
static PS temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(PS));
Component* x = &temp;
items.push_back(x);
}
items.pop_back();
f.close();
// read SSD objects into vector
f.open("SSD.txt", ios::binary);
while (!f.eof()) {
static SSD temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(SSD));
Component* x = &temp;
items.push_back(x);
}
// read RAM objects into vector
items.pop_back();
f.close();
f.open("RAM.txt", ios::binary);
while (!f.eof()) {
static RAM temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(RAM));
Component* x = &temp;
items.push_back(x);
}
items.pop_back();
f.close();
// read case objects into vector
f.open("Case.txt", ios::binary);
while (!f.eof()) {
static Case temp;
f.read(reinterpret_cast<char*>(&temp), sizeof(Case));
Component* x = &temp;
items.push_back(x);
}
items.pop_back();
f.close();
return items;
}
#ifndef COMPONENT_H
#define COMPONENT_H
#define SIZE 50
class Component {
protected:
int stock;
float price;
char name[SIZE];
char manufacturer[SIZE];
bool laptop;
public:
Component();
~Component();
void setManufacturer(const char*);
void setName(const char *);
void setPrice(float);
void setStock(int);
void setLaptop(bool);
virtual void printSpecs() = 0; // the pure virtual function
const char * getManufacturer() ;
const char * getName() ;
float getPrice() const;
int getStock() const;
bool getLaptop() const;
virtual int rank() = 0;
};
#endif
// One of the derived classes
void CPU::printSpecs() {
cout << "The specifications of this CPU are:\n"
<< "Name: " << getName() << endl
<< "Price: EUR " << price << endl
<< "Laptop: " << ((laptop == true) ? "yes" : "no" )<< endl
<< "Stock: " << stock << endl
<< "Manufacturer: " << getManufacturer() << endl
<< "Speed: " << speed << "GHz" << endl
<< "Cores: " << cores << endl;
}