Help with variable declared inside an if block

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 4
Reputation: pepipox is an unknown quantity at this point 
Solved Threads: 0
pepipox pepipox is offline Offline
Newbie Poster

Help with variable declared inside an if block

 
0
  #1
Jan 12th, 2008
Dear all
In advance, thank you for your help. I am trying to initialize an object in C++ depending on certain parameter. Below is the source code I am using.

if(ARG::metrn==metrnSMD)
EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain);
else
EventStream<Lambda> evstrTrain(fileTrain, evlsTrain);

if (ARG::FRandomEventOrder())
evstrTrain.SetRandomOrder(true);

I am declaring and calling the constructor for 'evstrTrain' inside the if block, however, in the second if this is not seen and the compiler generates an "undeclared identifier" error, for the part when evstrTrain.SetRandomOrder is called. I need to this quite a few times in my code, can anybody tell me a workaround for this please? I am using Visual Studio C++ 2005.

Regards
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Help with variable declared inside an if block

 
0
  #2
Jan 12th, 2008
the code in the if-else is equivalent to
  1. if(ARG::metrn==metrnSMD)
  2. { EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain); }
  3. else
  4. { EventStream<Lambda> evstrTrain(fileTrain, evlsTrain); }
so the variable is not visible outside the scope of the if or else blocks.

one commonly used work around is to have a polymorphic base class. this works if the method signatures are identical for all the template specializations. eg.
  1. #include <iostream>
  2.  
  3. struct event_stream_base
  4. {
  5. virtual ~event_stream_base() {} // this is the destructor;
  6. // there is a problem with the daniweb code formatter
  7. virtual void set_random_order(bool) = 0 ;
  8. } ;
  9.  
  10. template< typename T > struct event_stream : event_stream_base
  11. {
  12. explicit event_stream( const T& v ) : member(v) {}
  13. virtual void set_random_order(bool)
  14. { std::cout << member << '\n' ; }
  15. T member ;
  16. // ...
  17. };
  18.  
  19. int main()
  20. {
  21. int a = 8 ;
  22. event_stream_base* pesb = 0 ;
  23. if( a > 5 ) pesb = new event_stream<double>(2.34) ;
  24. else pesb = new event_stream<int>(73) ;
  25.  
  26. pesb->set_random_order(true) ;
  27.  
  28. delete pesb ;
  29. }
(another is to use a meta-function; but it may be prudent to ignore this option for now.)
Last edited by vijayan121; Jan 12th, 2008 at 3:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help with variable declared inside an if block

 
1
  #3
Jan 12th, 2008
I think the original problem (causing the compiler error) is this:
EventStream<complex<Lambda>> evstrTrain( ...
C++ compilers can't distinguish this from the right-shift operator. You need to have a space between them:
EventStream<complex<Lambda> > evstrTrain( ...

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Help with variable declared inside an if block

 
0
  #4
Jan 12th, 2008
> I think the original problem (causing the compiler error) is this:
> EventStream<complex<Lambda>> evstrTrain( ...
he is using Visual Studio C++ 2005. which accepts this (perhaps in anticipation of c++0x).
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: pepipox is an unknown quantity at this point 
Solved Threads: 0
pepipox pepipox is offline Offline
Newbie Poster

Re: Help with variable declared inside an if block

 
0
  #5
Jan 12th, 2008
Originally Posted by vijayan121 View Post
> I think the original problem (causing the compiler error) is this:
> EventStream<complex<Lambda>> evstrTrain( ...
he is using Visual Studio C++ 2005. which accepts this (perhaps in anticipation of c++0x).
Dear vijayan121

Thank you for your fast answer and your time. I saw your solution but I think is not viable. I am currently modifying a HUGE software project and trying to make a base class that emulates "eventstream" is not feasible.

Is there any other workaround?. Actually I am sort of surprised, what I intend is so simple, if (ARG::metrn) has some value declare EventStream evntstrTrain calling with the complex template, else declare it with the Lambda template.

I have been surfing the web last 2 hours but have not seen any workaround. Again, your help is appreciated.

To Douas, Thank you very much for the answer, but it is not a problem of syntax. The problem is of the scope of the variables, if I declare them inside an if block, the scope is local only to that if block, and I want them to be local to the whole function.

Thanks again.

Regards
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Help with variable declared inside an if block

 
0
  #6
Jan 12th, 2008
well, for the simple example you posted, you could write
  1. if(ARG::metrn==metrnSMD)
  2. {
  3. EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain);
  4. if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
  5. }
  6. else
  7. {
  8. EventStream<Lambda> evstrTrain(fileTrain, evlsTrain);
  9. if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
  10. }
but this may not be pragmatic in more complex cases (where the second if is not proximate to the first if-else)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: pepipox is an unknown quantity at this point 
Solved Threads: 0
pepipox pepipox is offline Offline
Newbie Poster

Re: Help with variable declared inside an if block

 
0
  #7
Jan 12th, 2008
Originally Posted by vijayan121 View Post
well, for the simple example you posted, you could write
  1. if(ARG::metrn==metrnSMD)
  2. {
  3. EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain);
  4. if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
  5. }
  6. else
  7. {
  8. EventStream<Lambda> evstrTrain(fileTrain, evlsTrain);
  9. if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
  10. }
but this may not be pragmatic in more complex cases (where the second if is not proximate to the first if-else)
Dear vijayan121
Again thanks for your fast answer.

As you said, this is not pragmatic in more complex cases. The problem is that I have more parts when I should do parameter dependent variable declaration, and in those cases this "super if" solution is also not feasible. Thank you again for your invaluable insights. Do you have any other idea?, this is for a very important job and the deadline is really soon. Sorry for any inconveniences.

Regards
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Help with variable declared inside an if block

 
0
  #8
Jan 12th, 2008
> what I intend is so simple, if (ARG::metrn) has some value declare EventStream evntstrTrain
> calling with the complex template, else declare it with the Lambda template.

if there are only a limited number of possibilities (like two in the example), you could use a variable of type boost::any http://www.boost.org/doc/html/any.html to hold the variable and do a type check later to figure out what it is.
  1. #include <iostream>
  2. #include <boost/any.hpp>
  3. #include <typeinfo>
  4.  
  5. template< typename T > struct event_stream
  6. {
  7. explicit event_stream( const T& v ) : member(v) {}
  8. void set_random_order(bool) { std::cout << member << '\n' ; }
  9. T member ;
  10. // ...
  11. };
  12.  
  13. int main()
  14. {
  15. int a = 8 ;
  16. boost::any any ;
  17. if( a > 5 ) any = event_stream<double>(2.34) ;
  18. else any = event_stream<int>(73) ;
  19.  
  20. // ...
  21.  
  22. if( any.type() == typeid( event_stream<double> ) )
  23. boost::any_cast< event_stream<double> >(
  24. any).set_random_order(true) ;
  25. else if( any.type() == typeid( event_stream<int> ) )
  26. boost::any_cast< event_stream<int> >(
  27. any).set_random_order(true) ;
  28. }
Last edited by vijayan121; Jan 12th, 2008 at 5:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help with variable declared inside an if block

 
0
  #9
Jan 12th, 2008
Nice! Boost rocks!

Sorry I wasn't paying attention when I responded earlier...
Last edited by Duoas; Jan 12th, 2008 at 5:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: pepipox is an unknown quantity at this point 
Solved Threads: 0
pepipox pepipox is offline Offline
Newbie Poster

Re: Help with variable declared inside an if block

 
0
  #10
Jan 12th, 2008
Dear vijayan121 and Duoas
Thank you very much for your interest. I am relatively new to C++. Though I have experience in C and other languages, I can fairly manage myself up to intermediate level programming. This advanced part on templates and other subtleties of C++ are new to me.

I haven't tried the boost solution since I will have to pass that variable to another function, and if it is wrapped into the 'any' variable I will have to modifiy everything. Due to my inexperience with C++, never thought it would turn out so extremely complex something I thought it was 'simple'. Also, this is a run once and forget software, so as long as it runs once or twice will be OK.

I will use the "super if" solution, since I am almost out of time.

Again, thank you very much for your kind time and explanations, deeply appreciate them.

Best regards
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC