944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1809
  • C++ RSS
Jan 12th, 2008
0

Help with variable declared inside an if block

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pepipox is offline Offline
4 posts
since Jan 2008
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

the code in the if-else is equivalent to
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jan 12th, 2008
1

Re: Help with variable declared inside an if block

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

> 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).
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
> 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pepipox is offline Offline
4 posts
since Jan 2008
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

well, for the simple example you posted, you could write
C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
well, for the simple example you posted, you could write
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pepipox is offline Offline
4 posts
since Jan 2008
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

> 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.
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

Nice! Boost rocks!

Sorry I wasn't paying attention when I responded earlier...
Last edited by Duoas; Jan 12th, 2008 at 5:20 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jan 12th, 2008
0

Re: Help with variable declared inside an if block

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pepipox is offline Offline
4 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: i dont know how to continue...
Next Thread in C++ Forum Timeline: c++ compiler for TI 89 calculator





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


Follow us on Twitter


© 2011 DaniWeb® LLC