I am a beginner in programming and C++.

Why my variables are not declared? I declare them with Initialize() func, and this function is before Update().

Are these variables not global?

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

Here are the errors:

In function 'void Update()' :
   error: 'squareText' was not declared in this scope
   error: 'curPlayerName' was not declared in this scope
有帮助吗?

解决方案

You have to declare both variables in global scope, that's to say before

void Initialize()

This way all your functions will be able to access these variables.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top