I have to read from a file and instantiate objects based on the line of the file I'm reading from.
The file has the format:
num1 RandomString RandomString num2 num3 num4<br>
num1 RandomString RandomString<br>
num1 RandomString RandomString num2 num3 num4<br>
The num1 identifies the type of object I need to instantiate. For example, if num1 is a 1, then I need to create a dog object, if it is anything else then I need to make a generic animal object.
The code below is halfway there.
NOTE: I haven't bothered with making members private at the moment. Not a priority right now.
#include <vector>
etc...
class Animal {
public:
int num1;
string 1;
string 2;
int num2;
int num3;
int num4;
std::string restOfTheLine;
Animal(fLine){
std::stringstream stream(fLine)
stream >> num1;
std::getline(stream, restOfTheLine);
getFunctions etc...
}
class Dog : public Animal{
}
int main(){
ifstream file("file.txt");
std::string line;
std::vector<Animal*> animals;
while(!file.eof()){
std::getline(file,line);
if(line[0]=='1'){
Dog* dog = new Dog(line);
animals.push_back(dog);
}
else{
Animal* animal = new Animal(line);
animals.push_back(animal);
}
std::cout << animals[2]->getNum2();
}
so I'm checking if the first character of the line is a 1. If it is, then a dog object has to be instantiated.
When I compile it gives me an error - error:no matching function for call dog::dog
, which is obviously because I haven't written anything in the derived dog class because I'm unsure how to approach it.
I have a vector of type pointer to animal, so how can I add a pointer to dog using inheritance?