c++ - 为什么我会收到编译错误 "<variable> not declared in this scope"?

标签 c++ compilation

不久前,我的第一个 C++ 程序(这个)遇到了一些问题。基本上,我正在尝试为教授没有教我们语法的 C++ 类(class)做一个介绍作业。这是我的代码:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>


using namespace std;
class Race
{
  public:
     void main()
     {

        executeRace();

        int randomMove()
        {
                srand(time(NULL));
                int randomInt = rand() % 100 + 1;
                return randomInt;
        }

        void executeRace()
        {
                int rabbitPosition = 1;
                int turtlePosition = 1;

                cout << "BANG!!!" << endl << "AND THEY'RE OFF!!!";

                while (rabbitPosition <=70 && turtlePosition <=70)
                {
                        printPositions(rabbitPosition, turtlePosition);

                        turtlePosition = turtleMoveSquares(turtlePosition);
                        rabbitPosition = rabbitMoveSquares(rabbitPosition);
                }

                printWinner(rabbitPosition, turtlePosition);

                tie(rabbitPosition, turtlePosition);
        }

        int turtleMoveSquares(int tPosition)
        {

                int turtleMove = randomMove();

                if(turtleMove >=1 && turtleMove <= 40)
                        tPosition = tPosition + 4;

                if(turtleMove >= 41 && turtleMove <= 50 )
                        tPosition = tPosition - 2;

                if(turtleMove >=51 && turtleMove <=100)
                        tPosition = tPosition + 2;

                if(tPosition < 1)
                        tPosition = 1;

                return tPosition;
        }

        int rabbitMoveSquares(int rabbitPosition)
        {

                int rabbitMove = randomMove();

                if(rabbitMove >=1 && rabbitMove <= 25)
                        rabbitPosition = rabbitPosition;

                if(rabbitMove >=26 && rabbitMove <= 55)
                        rabbitPosition = rabbitPosition + 10;

                if(rabbitMove >=56 && rabbitMove <=60)
                        rabbitPosition = rabbitPosition - 15;

                if(rabbitMove >=61 && rabbitMove <= 90)
                        rabbitPosition = rabbitPosition + 5;

                if(rabbitMove >=90 && rabbitMove <=100)
                        rabbitPosition = rabbitPosition - 3;

                if(rabbitPosition < 1)
                        rabbitPosition = 1;

                return rabbitPosition;
        }

        void printPositions(int rabbitPositions, int turtlePositions)
        {
                int turtleCount;
                int rabbitCount;
                int endCount;

                if(rabbitPositions == turtlePositions && rabbitPositions != 1)
                {
                        turtleCount = 1;

                        while(turtleCount < turtlePositions)
                        {
                                cout << "-";
                                turtleCount = turtleCount+1;
                        }
                        cout << "OUCH!";
                }

                else
                {
                        turtleCount = 1;
                        rabbitCount = 1;
                        endCount=1;

                        if(turtlePositions < rabbitPositions)
                        {
                                while(turtleCount < turtlePositions)
                                {
                                        cout <<  "-";
                                                turtleCount = turtleCount+1;
                                }
                                cout << "T";

                                while(rabbitCount < (rabbitPositions - turtlePositions))
                                {
                                        cout <<  "-";
                                        rabbitCount = rabbitCount+1;
                                }
                                cout << "H";

                        }

                        if(rabbitPositions < turtlePositions)
                        {
                                while(rabbitCount < rabbitPositions)
                                {
                                        cout << "-";
                                                rabbitCount = rabbitCount+1;
                                }
                                cout << "H";

                                while(turtleCount < (turtlePositions - rabbitPositions))
                                {
                                        cout << "-";
                                        turtleCount = turtleCount+1;
                                }
                                cout << "T";

                                cout << "\n";
                        }
                }
        }

        void printWinner(int rabbitPosition, int turtlePosition)
        {
                if(turtlePosition >= 70 && rabbitPosition < 70)
                {
                        cout << "TORTOISE WINS!!! YAY!!!\n";
                }
                else if(rabbitPosition >=70 && turtlePosition < 70)
                {
                        cout << "Hare wins. Yuch.\n";
                }
                else if(rabbitPosition >=70 && turtlePosition >=70)
                {
                        cout << "It's a tie\n";
                }
        }

        void tie(int turtlePosition, int rabbitPosition)
        {
                if(rabbitPosition >=70 && turtlePosition >=70)
                        executeRace();
        }

    }
};



int main()
{
  Race race;
  race.main();
  return EXIT_SUCCESS;
}

下面是我的编译错误:

uxb3% g++ o- Race Race.cc
g++: o-: No such file or directory
g++: Race: No such file or directory
Race.cc: In member function 'void Race::main()':
Race.cc:14: error: 'executeRace' was not declared in this scope
Race.cc:17: error: a function-definition is not allowed here before '{' token
Race.cc:24: error: a function-definition is not allowed here before '{' token
Race.cc:44: error: a function-definition is not allowed here before '{' token
Race.cc:64: error: a function-definition is not allowed here before '{' token
Race.cc:90: error: a function-definition is not allowed here before '{' token
Race.cc:153: error: a function-definition is not allowed here before '{' token
Race.cc:169: error: a function-definition is not allowed here before '{' token

很抱歉一直为这个任务打扰你们,但这是我的第一次,我现在非常非常沮丧和着迷。

最佳答案

你不能有 functions inside your functions *.

你可能想要这个:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>


using namespace std;
class Race
{
  public:
    int randomMove()
    {
            srand(time(NULL));
            int randomInt = rand() % 100 + 1;
            return randomInt;
    }

    void executeRace()
    {
            int rabbitPosition = 1;
            int turtlePosition = 1;

            cout << "BANG!!!" << endl << "AND THEY'RE OFF!!!";

            while (rabbitPosition <=70 && turtlePosition <=70)
            {
                    printPositions(rabbitPosition, turtlePosition);

                    turtlePosition = turtleMoveSquares(turtlePosition);
                    rabbitPosition = rabbitMoveSquares(rabbitPosition);
            }

            printWinner(rabbitPosition, turtlePosition);

            tie(rabbitPosition, turtlePosition);
    }

    int turtleMoveSquares(int tPosition)
    {

            int turtleMove = randomMove();

            if(turtleMove >=1 && turtleMove <= 40)
                    tPosition = tPosition + 4;

            if(turtleMove >= 41 && turtleMove <= 50 )
                    tPosition = tPosition - 2;

            if(turtleMove >=51 && turtleMove <=100)
                    tPosition = tPosition + 2;

            if(tPosition < 1)
                    tPosition = 1;

            return tPosition;
    }

    int rabbitMoveSquares(int rabbitPosition)
    {

            int rabbitMove = randomMove();

            if(rabbitMove >=1 && rabbitMove <= 25)
                    rabbitPosition = rabbitPosition;

            if(rabbitMove >=26 && rabbitMove <= 55)
                    rabbitPosition = rabbitPosition + 10;

            if(rabbitMove >=56 && rabbitMove <=60)
                    rabbitPosition = rabbitPosition - 15;

            if(rabbitMove >=61 && rabbitMove <= 90)
                    rabbitPosition = rabbitPosition + 5;

            if(rabbitMove >=90 && rabbitMove <=100)
                    rabbitPosition = rabbitPosition - 3;

            if(rabbitPosition < 1)
                    rabbitPosition = 1;

            return rabbitPosition;
    }

    void printPositions(int rabbitPositions, int turtlePositions)
    {
            int turtleCount;
            int rabbitCount;
            int endCount;

            if(rabbitPositions == turtlePositions && rabbitPositions != 1)
            {
                    turtleCount = 1;

                    while(turtleCount < turtlePositions)
                    {
                            cout << "-";
                            turtleCount = turtleCount+1;
                    }
                    cout << "OUCH!";
            }

            else
            {
                    turtleCount = 1;
                    rabbitCount = 1;
                    endCount=1;

                    if(turtlePositions < rabbitPositions)
                    {
                            while(turtleCount < turtlePositions)
                            {
                                    cout <<  "-";
                                            turtleCount = turtleCount+1;
                            }
                            cout << "T";

                            while(rabbitCount < (rabbitPositions - turtlePositions))
                            {
                                    cout <<  "-";
                                    rabbitCount = rabbitCount+1;
                            }
                            cout << "H";

                    }

                    if(rabbitPositions < turtlePositions)
                    {
                            while(rabbitCount < rabbitPositions)
                            {
                                    cout << "-";
                                            rabbitCount = rabbitCount+1;
                            }
                            cout << "H";

                            while(turtleCount < (turtlePositions - rabbitPositions))
                            {
                                    cout << "-";
                                    turtleCount = turtleCount+1;
                            }
                            cout << "T";

                            cout << "\n";
                    }
            }
    }

    void printWinner(int rabbitPosition, int turtlePosition)
    {
            if(turtlePosition >= 70 && rabbitPosition < 70)
            {
                    cout << "TORTOISE WINS!!! YAY!!!\n";
            }
            else if(rabbitPosition >=70 && turtlePosition < 70)
            {
                    cout << "Hare wins. Yuch.\n";
            }
            else if(rabbitPosition >=70 && turtlePosition >=70)
            {
                    cout << "It's a tie\n";
            }
    }

    void tie(int turtlePosition, int rabbitPosition)
    {
            if(rabbitPosition >=70 && turtlePosition >=70)
                    executeRace();
    }
};

int main()
{
  Race race;
  race.executeRace();
  return EXIT_SUCCESS;
}

*当然,除非处理高级语言,例如微积分!

关于c++ - 为什么我会收到编译错误 "<variable> not declared in this scope"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/526817/

相关文章:

c++ - 如何将父类转换为子类

c++ - 如何到达 2d std::vector (`std::vector< std::vector<T>>` ) 的第 N 个元素?

c++ - CMake : softlink resource ( such as GLSL shaders ) or copy each complilation

javascript - Haxe JS 编译器从源代码中删除一个属性

java - 构建快照与本地编译代码的争论?

c++ - 彭博 API : field not permitted to datafeed users

c++ - as_ldt_init 初始化失败——C++ 客户端

java - 编译并在内存中执行java源文件

c - 什么是 .dbg 文件以及它们与 gcc 中的 `-g` 选项有什么关系

java - 无法使用 apktool : "Asset package include not found" 编译应用程序