944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2893
  • C++ RSS
Sep 11th, 2007
0

Variable scope problem

Expand Post »
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...
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Sep 11th, 2007
0

Re: Variable scope problem

I not at all clear with the solution you have provided . Kindly give some sample code for my problem,
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Sep 11th, 2007
0

Re: Variable scope problem

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
redbaron is offline Offline
2 posts
since Sep 2007
Sep 11th, 2007
0

Re: Variable scope problem

>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:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. // ...
  4. Graph g1;
  5.  
  6. if ( var )
  7. g1 = CreateGraph<Graph> ( vertex, edge );
  8. else
  9. g1 = CreateGraph<Graph1> ( vertex, edge );
  10.  
  11. numvertex ( g1 );
  12. }
>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:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. // ...
  4.  
  5. {
  6. Graph g1;
  7.  
  8. if ( var )
  9. g1 = CreateGraph<Graph> ( vertex, edge );
  10. else
  11. g1 = CreateGraph<Graph1> ( vertex, edge );
  12.  
  13. numvertex ( g1 );
  14. }
  15. }
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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 11th, 2007
0

Re: Variable scope problem

If you would look closely he has 2 different Graph classes apparently, Graph and Graph1

C++ Syntax (Toggle Plain Text)
  1. if(var==true)
  2. {
  3. Graph g1=CreateGraph<Graph>(vertex,edge);
  4. }
  5. else
  6. {
  7. Graph1 g1=CreateGraph<Graph1>(vertex,edge);
  8. }

I initially posted the response that you had, before noticing that
Reputation Points: 10
Solved Threads: 0
Newbie Poster
redbaron is offline Offline
2 posts
since Sep 2007
Sep 11th, 2007
0

Re: Variable scope problem

>If you would look closely he has 2 different Graph classes apparently, Graph and Graph1
*sigh* I hate it when poor naming practices cause me to make a mistake.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: general question
Next Thread in C++ Forum Timeline: Code not adding my numbers correctly...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC