错误:“my var”未在此范围内声明

时间:2014-03-30 22:01:09

标签: variables scope tic-tac-toe

我是编程和C ++的初学者。

为什么我的变量没有被声明?我用Initialize()函数声明它们,这个函数在Update()之前。

这些变量不是全局的吗?

#include <iostream>

using namespace std;


void Initialize()
{
    string curPlayerName = "PlayerX";

    char squareText[9] = {'x', 'a'};

    int input;
}

void Update()
{
    cout << "\n\n \t\t" << "TIC TAC TOE" << endl;

    cout << "\n \t" << "     |     |     ";
    cout << "\n \t" << "  " >> squareText[0] >> "  |  1  |  2  ";
    cout << "\n \t" << "_____|_____|_____";
    cout << "\n \t" << "     |     |     ";
    cout << "\n \t" << "  3  |  4  |  5  ";
    cout << "\n \t" << "_____|_____|_____";
    cout << "\n \t" << "     |     |     ";
    cout << "\n \t" << "  6  |  7  |  8  ";
    cout << "\n \t" << "     |     |     ";

    cout << "\n\n \t" << curPlayerName << ", enter a number:" << endl;
}

int main()
{
    Initialize();
    Update();
    /*cin >> input;

    switch(input)
    {
    case 0:
        break;
    }*/


    cout << "\n\n\n";
    return 0;
}

以下是错误:

In function 'void Update()' :
   error: 'squareText' was not declared in this scope
   error: 'curPlayerName' was not declared in this scope

1 个答案:

答案 0 :(得分:0)

您必须在全局范围内声明这两个变量,并在

之前说明
void Initialize()

这样,您的所有功能都可以访问这些变量。