?
Solved

how define global var between { and }

Posted on 2002-05-30
15
Medium Priority
?
192 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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

Industry Leaders: 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!

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…
Suggested Courses

770 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