I am attempting to make a vector that stores a base class and then passes it to another class which then accesses the derived class from the vector of base classes I have found multiple Stack Overflow questions covering this but they are all missing certain aspects like passing this across classes or storing multiple objects in a vector. CPP File
vector<Item*> Items::cM() {
vector<Item*> M;
string line;
string filePath = "...";
ifstream stream(filePath);
while (getline(stream, line)) {
vector <string> IV= UtilFunctions::splitString(line, ',');
const char *t= IV[0].c_str();
switch (*t) {
case 'a': {
StarterFood tmpStarter = StarterFood(*a,b,c);//Simplified
Item* b = &tmpStarter;
M.push_back(b);
//If I use b here and do b->toString() works as expected
break;
}
}
}
return M;
}
Main
int main(){
vector <Item*> i= Items::cM();
items[0]->toString();//This is an overloaded function that all of the derived classes
//Throws the error Access violation reading location 0xCCCCCCCC.
have such as starterfood
system("pause");
return 0;
}
If anymore information is needed please feel free to ask. Thanks I have also tried passing a pointer and then de referencing the pointer but I think that slices my object leaving just the base class and I have tried to implement unique_ptr but i get a syntax error saying there is no overload that returns unique_ptr from starterFood. The error is Access violation reading location 0xCCCCCCCC.