2014-03-30 32 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() 

這樣,所有的功能將能夠訪問這些變量。