我的指针向量有什么问题?

时间:2014-04-25 23:56:45

标签: c++ vector

我正在为一个课堂作业做一个小游戏,我已经陷入困境。我以为我的矢量设置正确,但它说它未定义。我正在尝试制作动物指针的矢量,因为它是一只乌龟和野兔种族。

他们有4个头文件和5个cpp文件。我只会做2我遇到问题,如果你想看到别人问的话。它被设置为野兔类,乌龟类。这继承了阶级动物的特征。然后比赛是比赛的实际模拟。这就是我遇到问题的地方。

问题在于我尝试制作矢量。在种族.h文件中我的void drawfinishline函数声明下面。

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

}

0 个答案:

没有答案