How to create a global object

if (choice ==1){
Light *myobj = new Light();
}
FlipUpCommand switchUp(*myobj);

Error: `myobj' undeclared (first use this function)

Please advice how to solve this problem without changing the Light class.
Scope of "myobj" is if-block, so outer code can't access it. You should move myobj out.
1
2
3
4
5
6
7
Light *myobj = NULL;
if (choice ==1)
{
    Light *myobj = new Light();
}
if(myobj)
    FlipUpCommand switchUp(*myobj);
Last edited on
one more way
1
2
3
4
if (choice ==1){
 Light *myobj = new Light();
 FlipUpCommand switchUp(*myobj);
 }

@r4zzz4k
1
2
3
4
5
6
7
Light *myobj = NULL;
if (choice ==1)
{
    Light *myobj = new Light(); // otherwise myobj remains NULL
}
if(myobj)
    FlipUpCommand switchUp(*myobj);
Sorry for my inattention. coder777 is right, of course, no second pointer inside must be declared.
Topic archived. No new replies allowed.