2014-03-30 34 views
-1

我是编程和C++的初学者。错误:“我的var”未在此范围内声明

为什么我的变量没有声明?我用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 
+0

阅读一本好的C++编程书。将'Initialize'中的声明移到全局范围。 –

回答

0

您必须声明在全局范围内这两个变量,这是说之前

void Initialize() 

这样,所有的功能将能够访问这些变量。