Solved

how define global var between { and }

Posted on 2002-05-30
15
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

726 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