Solved

how define global var between { and }

Posted on 2002-05-30
15
188 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using Diagnostic tools in VS2015: Unresoved allocations 19 85
Swiper slider stops between pages 11 50
cookies analysis tools 2 93
Beginner to Unreal Engine 4 5 80
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

831 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