DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Variable scope problem (http://www.daniweb.com/forums/thread89250.html)

tonyaim83 Sep 11th, 2007 6:48 am
Variable scope problem
 
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...

tonyaim83 Sep 11th, 2007 7:40 am
Re: Variable scope problem
 
I not at all clear with the solution you have provided . Kindly give some sample code for my problem,

redbaron Sep 11th, 2007 10:35 am
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...

Narue Sep 11th, 2007 4:04 pm
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:
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?

redbaron Sep 11th, 2007 5:01 pm
Re: Variable scope problem
 
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 :S

Narue Sep 11th, 2007 7:41 pm
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.


All times are GMT -4. The time now is 4:04 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC