• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

how define global var between { and }

{static int testing=0;}
if (testing != 0) ...

generates: error C2065: 'testing' : undeclared identifier

however I need to define that var between { } otherwise I get:

error C2143: syntax error : missing ';' before 'type'
0
mortonsw
Asked:
mortonsw
  • 5
  • 5
  • 5
1 Solution
 
AxterCommented:
Where in your code do you have this?
Is it in a function?
0
 
AxterCommented:
I think what you're trying to do is defined a file scope variable.

That can be done by using namespace, and removing the keyword static.
Example:
namespace
{
  int testing=0;
}

0
 
AxterCommented:
The above namespace method should be located in your *.cpp file.

If you're trying to declare a static variable in your function, and your getting those errors, then there's something else wrong with the code.
If this is the case, please post the code for the function that you're trying to add a static variable for.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
My previous comment is a little ambiguous.

Let me clarify.  You can not use {} around a declared variable, and then attempt to use outside of the brackets.
The variable only exist within the scope of the brackets.
Even if the variable is static, the code outside the brackets don't have DIRECT access to it.

So you need to remove the brackets to access the variables.
If you're getting a compile error when your remove the brackets, then there's something else wrong with the code, and you should post you function so we can have a better chance of pointing out the offending code.
0
 
mortonswAuthor Commented:
Simply put: I need to declare a variable in my function NOT at the top. The variable will be used only in code that follows it but may or may not have code/data that precedes it.
0
 
AxterCommented:
>>Simply put: I need to declare a variable in my function
>>NOT at the top.

Then you have to included in your function WITHOUT the brackets.

If you're getting a compiler error because you're removing the brackets, then there is something else wrong with your code.

Please post the function.
0
 
DanRollinsCommented:
>>error C2143: syntax error : missing ';' before 'type'

This means that you forgot a semicolon or made some other syntax error that made the compiler not expect to see the word 'static'.  You will need to post the code aboce that line if you want us to figure out what's going on.

Wrapping curly brackets around a variable declaration makes that variable exist only within the defined scope.  That's how it is supposed to work.

-- Dan
0
 
DanRollinsCommented:
You can also use:
{
    static int testing=0;
    if (testing != 0) ...
    // other code that uses the variable...
}

// the variable 'testing' is out of scope again

-- Dan
0
 
mortonswAuthor Commented:
The program is an MSVC example comdlg32.c. It compiles w/no errors until I insert the line: int testing=0;

It then generates error: error C2143: syntax error : missing ';' before 'type'

Error message refering to line I added (int testing=0;).


int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow )
{

    MSG msg;                        

    if (!InitApplication(hInstance))
            return (FALSE);              

    // Create the main window.
    if (!InitInstance(hInstance, nCmdShow))
        return (FALSE);

    int testing = 0; //line added by me

    // Acquire and dispatch messages until a WM_QUIT message is received.
    while (GetMessage(&msg,NULL,0,0))
        {
            TranslateMessage(&msg);    // Translates virtual key codes.
            DispatchMessage(&msg);     // Dispatches message to window.
        }
    return (msg.wParam);           // Returns the value from PostQuitMessage.

}
0
 
mortonswAuthor Commented:
The program is an MSVC example comdlg32.c. It compiles w/no errors until I insert the line: int testing=0;

It then generates error: error C2143: syntax error : missing ';' before 'type'

Error message refering to line I added (int testing=0;).


int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow )
{

    MSG msg;                        

    if (!InitApplication(hInstance))
            return (FALSE);              

    // Create the main window.
    if (!InitInstance(hInstance, nCmdShow))
        return (FALSE);

    int testing = 0; //line added by me

    // Acquire and dispatch messages until a WM_QUIT message is received.
    while (GetMessage(&msg,NULL,0,0))
        {
            TranslateMessage(&msg);    // Translates virtual key codes.
            DispatchMessage(&msg);     // Dispatches message to window.
        }
    return (msg.wParam);           // Returns the value from PostQuitMessage.

}
0
 
DanRollinsCommented:
If it is a C program (and not a C++ program) then it is not allowed to declare variables except at the top of a scope.

Rename the file to comdlg32.cpp and your troubles are over.

-- Dan
0
 
DanRollinsCommented:
Anyway, why not just put the declration at the top of the scope, like a good C programmer?
0
 
DanRollinsCommented:
I suggest that you will need to post this in the C programming Topic Area, becasue us C++ programmers don't have a clue.

-- Dan
0
 
mortonswAuthor Commented:
That did the trick!
0
 
mortonswAuthor Commented:
c:>ren comdlg32.c *.cpp
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now