0

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());

}
Grzegorz Adam Kowalski
  • 4,811
  • 2
  • 27
  • 37
user3530147
  • 1
  • 1
  • 3
  • The `do` loop in `promptForContestantNumbers()` doesn't make any sense. Read those lines carefully. As for the vector problem, you didn't say which line was flagged with the error and the exact error message. – ooga Apr 26 '14 at 00:19
  • I just changed it to (while animalCount > 8) this makes sense now lol because I want it to keep prompting them until they enter a number below 8. – user3530147 Apr 26 '14 at 00:24
  • The issue is right underneath the void drawfinishline declaration where I instantiate the vector in the race.h file – user3530147 Apr 26 '14 at 00:25
  • 1
    Just a guess, but should it be `Animal` instead of `Animals`? (Seems like a better name, anyway.) – ooga Apr 26 '14 at 00:26
  • 1
    vector vAnimals; it says that Animals is undefined. – user3530147 Apr 26 '14 at 00:26
  • I bet it's that `s` at the end then! – ooga Apr 26 '14 at 00:27
  • wow so simple. Thanks ooga! I can't believe it was that lol – user3530147 Apr 26 '14 at 00:28
  • It's easy to miss something that small. Good luck! – ooga Apr 26 '14 at 00:28
  • I compile and everything works but then it says mismatch detected MSC_ver 1600 is not 1800 or something like that. – user3530147 Apr 26 '14 at 00:33
  • Read this: http://stackoverflow.com/questions/19575747/error-lnk2038-mismatch-detected-for-msc-ver-value-1600-doesnt-match-valu – ooga Apr 26 '14 at 00:35

0 Answers0