User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,602 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,515 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1184 | Replies: 5
Reply
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Variable scope problem

  #1  
Sep 11th, 2007
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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Variable scope problem

  #2  
Sep 11th, 2007
>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:
int main()
{
  // ...
  Graph g1;

  if ( var )
    g1 = CreateGraph<Graph> ( vertex, edge );
  else
    g1 = CreateGraph<Graph1> ( vertex, edge );

  numvertex ( g1 );
}
>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:
int main()
{
  // ...

  {
    Graph g1;

    if ( var )
      g1 = CreateGraph<Graph> ( vertex, edge );
    else
      g1 = CreateGraph<Graph1> ( vertex, edge );

    numvertex ( g1 );
  }
}
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?
I'm here to prove you wrong.
Reply With Quote  
Join Date: Sep 2007
Posts: 2
Reputation: redbaron is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
redbaron redbaron is offline Offline
Newbie Poster

Re: Variable scope problem

  #3  
Sep 11th, 2007
If you would look closely he has 2 different Graph classes apparently, Graph and Graph1

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
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Variable scope problem

  #4  
Sep 11th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: Variable scope problem

  #5  
Sep 11th, 2007
I not at all clear with the solution you have provided . Kindly give some sample code for my problem,
Reply With Quote  
Join Date: Sep 2007
Posts: 2
Reputation: redbaron is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
redbaron redbaron is offline Offline
Newbie Poster

Re: Variable scope problem

  #6  
Sep 11th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC