Solved

how define global var between { and }

Posted on 2002-05-30
15
186 Views
Last Modified: 2013-11-18
{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
Comment
Question by:mortonsw
  • 5
  • 5
  • 5
15 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7045560
Where in your code do you have this?
Is it in a function?
0
 
LVL 30

Expert Comment

by:Axter
ID: 7045562
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
 
LVL 30

Expert Comment

by:Axter
ID: 7045565
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
 
LVL 30

Expert Comment

by:Axter
ID: 7045571
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7045658
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
 
LVL 30

Expert Comment

by:Axter
ID: 7045666
>>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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7045680
>>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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 49

Expert Comment

by:DanRollins
ID: 7045683
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7045707
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7045709
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
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 7045734
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7045735
Anyway, why not just put the declration at the top of the scope, like a good C programmer?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7045736
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7046179
That did the trick!
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7046182
c:>ren comdlg32.c *.cpp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now