| | |
Variable scope problem
![]() |
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
Hi
I have the following code ...
main()
{
....
...
if(var==true)
{
Graph g1=CreateGraph<Graph>(vertex,edge);
}
else
{
Graph1 g1=CreateGraph<Graph1>(vertex,edge);
}
numvertex(g1);
}
When i m calling numvertex() function it says undefined identifier g1 what can be the solution for this. Provided I cannot make define the object globally or at the start of main(). Is there anyway out for this. I also tried making this static but no luck . Means the object scope ends as the If block ends.Plz help...
I have the following code ...
main()
{
....
...
if(var==true)
{
Graph g1=CreateGraph<Graph>(vertex,edge);
}
else
{
Graph1 g1=CreateGraph<Graph1>(vertex,edge);
}
numvertex(g1);
}
When i m calling numvertex() function it says undefined identifier g1 what can be the solution for this. Provided I cannot make define the object globally or at the start of main(). Is there anyway out for this. I also tried making this static but no luck . Means the object scope ends as the If block ends.Plz help...
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
Without seeing all the other code it is difficult to see what you are doing... One quick fix would be to add the function calls within the if statements. If you posted more about these Graph and Graph1 classes we would be able to help you find a better solution...
Last edited by redbaron; Sep 11th, 2007 at 11:41 am.
>Provided I cannot make define the object globally or at the start of main().
You have to define a variable before it's used, and you have to define a variable within a visible scope. What you're doing is illegal because g1 is defined in a nested scope but used beyond that scope. It has to be like this:
>Provided I cannot make define the object globally or at the start of main().
If the unnamed block trick doesn't count, you're SOL:
However, I fail to understand why you have this restriction. Perhaps you can enlighten us as to what program requirements are stopping you from defining a variable in the correct scope?
You have to define a variable before it's used, and you have to define a variable within a visible scope. What you're doing is illegal because g1 is defined in a nested scope but used beyond that scope. It has to be like this:
C++ Syntax (Toggle Plain Text)
int main() { // ... Graph g1; if ( var ) g1 = CreateGraph<Graph> ( vertex, edge ); else g1 = CreateGraph<Graph1> ( vertex, edge ); numvertex ( g1 ); }
If the unnamed block trick doesn't count, you're SOL:
C++ Syntax (Toggle Plain Text)
int main() { // ... { Graph g1; if ( var ) g1 = CreateGraph<Graph> ( vertex, edge ); else g1 = CreateGraph<Graph1> ( vertex, edge ); numvertex ( g1 ); } }
I'm here to prove you wrong.
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
If you would look closely he has 2 different Graph classes apparently, Graph and Graph1
I initially posted the response that you had, before noticing that
C++ Syntax (Toggle Plain Text)
if(var==true) { Graph g1=CreateGraph<Graph>(vertex,edge); } else { Graph1 g1=CreateGraph<Graph1>(vertex,edge); }
I initially posted the response that you had, before noticing that
![]() |
Similar Threads
- Problem passing variables to thread (Perl)
- Is it a problem due to scope or compiler ? (C++)
- Problem with memory allocation =( (C)
- Scope Problem (C++)
- I just can't get this Javascript (JavaScript / DHTML / AJAX)
- variable environment (*nix Software)
- Variable Variable Names (C)
- Ie Variable Passing Problem**please Help** (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: general question
- Next Thread: Code not adding my numbers correctly...
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






