简体   繁体   English

为什么我会收到编译错误“ <variable> 没有在此范围内声明”?

[英]Why am I getting compile errors “<variable> not declared in this scope”?

I was having some problems with my first C++ program (this one) awhile ago. 前一段时间,我的第一个C ++程序遇到了一些问题。 Basically I am trying to do an assignment for intro to C++ class where the professor has taught us no syntax. 基本上,我试图做一个C ++课入门课程,在该课程中教授没有教我们语法。 Here is my code right now: 现在是我的代码:

#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;
}

and here are my errors on compile: 这是我的编译错误:

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

Sorry to keep bothering you guys about this assignment, but it is my first and I am very, VERY frustrated and obsessed at the moment. 抱歉让您困扰我,但这是我的第一次,现在我非常沮丧和痴迷。

You cannot have functions inside your functions *. 的函数 *中不能有函数

You probably want this: 您可能想要这样:

#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;
}

*Unless dealing with higher-level languages, such as calculus, of course! *当然,除非处理诸如演算之类的高级语言!

Compiler options: 编译器选项:

g++ -o Race Race.cc

You're also declaring functions within your Race::main function 您还需要在Race :: main函数中声明函数

class Race
{
  public:
     void main()
     {

        executeRace();
     } // <----- add this

I think you should be using gcc -o ... , not gcc o- ... . 我认为您应该使用gcc -o ...而不是gcc o- ...

And, secondly, you can't define functions withing functions in C++. 其次,您不能使用C ++中的函数定义函数。

Move the other function definition outside of your class void main() : specifically you need to move the second last brace before your int main() to immediately before int randomMove() . 将另一个函数定义移到类void main() :特别是,您需要将int main()之前的倒数第二大括号移到int randomMove()之前。

You want to pull out the various functions which are at the moment included in the main() function defined in Race (inside its braces). 您想拉出Race中定义的main()函数中包含的各种函数(在其花括号内)。

Select them, Ctrl-X (Cut), move to above the main() declaration, Ctrl-V (Paste). 选择它们,Ctrl-X(剪切),移到main()声明Ctrl-V(粘贴)上方。 Reformat. 重新格式化。 Recompile. 重新编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM