I am making a little game for a class assignment and I have come to a impass. I thought I had my vector set up right but it says it is undefined. I am trying to make a vector of animal pointers because it is a tortoise and a hare race.
There are about their are 4 header files and 5 cpp files. I will only do the 2 I am having a problem with, if you want to see the others just ask. It is set up as a class for hare, a class for tortoise. That inherit traits from class animal. And then race is the actual simulation of the race. Which is where I am having problems.
The issue is where I try to make the vector. right underneath my void drawfinishline function declaration in the race .h file.
Race.h
#ifndef _RACE_H_
#define _RACE_H_
#include "stdafx.h"
#include "Hare.h"
#include "Animal.h"
#include "Tortoise.h"
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include "Textbox.h"
#include "Delay.h"
#include <iomanip>
#include <vector>
using namespace std;
class Race
{
private:
int winningDistance;
void drawFinishLine();
vector<Animals*> vAnimals;
int hareCount;
int tortoiseCount;
int animalCount;
int distances[30];
void promptForContestantNumbers();
int GetValidIntegerConsole(string prompt);
bool winnerExists();
public:
Race();
~Race();
void run();
};
#endif
Race.cpp
#include "stdafx.h"
#include "Race.h"
#include "Delay.h"
Race::Race()
{
srand(time(0));
winningDistance = 22;
hareCount = 0;
tortoiseCount = 0;
animalCount = 0;
for (int i = 0; i<8; i++)
{
distances[i] = 0;
}
}
Race::~Race()
{
for (int i = 0; i<animalCount; i++)
{
delete vAnimals.at(i);
}
}
int Race::GetValidIntegerConsole(string prompt)
{
cout << prompt << endl;
int number;
cin >> number;
while (cin.fail())
{
cin.clear();
cin.ignore(10, '\n');
cout << prompt;
cin >> number;
}
return number;
}
void Race::drawFinishLine()
{
for (int y = 0; y < 25; y += 3)
{
gotoxy(winningDistance, y);
cout << "|";
}
}
void Race::promptForContestantNumbers()
{
do
{
animalCount = GetValidIntegerConsole("How many weights (<=8): ");
hareCount = GetValidIntegerConsole("How many weights (<=8): ");
tortoiseCount = GetValidIntegerConsole("How many weights (<=8): ");
animalCount = animalCount + hareCount + tortoiseCount;
} while (animalCount>30);
Animals* p_ANIMALS;
p_ANIMALS = NULL;
for (int i = 0; i<hareCount; i++)
{
p_ANIMALS = new Hare;
vAnimals.push_back(p_ANIMALS);
}
for (int i = 0; i<tortoiseCount; i++)
{
p_ANIMALS = new Tortoise;
vAnimals.push_back(p_ANIMALS);
}
}
bool Race::winnerExists()
{
for (int i = 0; i<8; i++)
{
if (distances[i] >winningDistance)
{
return true;
}
}
return false;
}
void Race::run()
{
promptForContestantNumbers();
system("cls");
drawFinishLine();
for (int i = 0; i<animalCount; i++)
{
vAnimals.at(i)->setTopSpeed(rand() % 4 + 1);
}
for (int i = animalCount; i<animalCount + hareCount; i++)
{
vAnimals.at(i)->setTopSpeed(rand() % 3 + 1);
}
for (int i = 0; i<animalCount; i++)
{
vAnimals.at(i)->display(i * 2 + 1, distances[i]);
}
do
{
for (int i = 0; i<animalCount; i++)
{
vAnimals.at(i)->erase(i * 2 + 1, distances[i]);
}
for (int i = 0; i<animalCount; i++)
{
distances[i] += vAnimals.at(i)->drop();
}
for (int i = 0; i<animalCount; i++)
{
vAnimals.at(i)->display(i * 2 + 1, distances[i]);
}
delay(300);
} while (!winnerExists());
}