| | |
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:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
the code in the if-else is equivalent to
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.
(another is to use a meta-function; but it may be prudent to ignore this option for now.)
C++ Syntax (Toggle Plain Text)
if(ARG::metrn==metrnSMD) { EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain); } else { EventStream<Lambda> evstrTrain(fileTrain, evlsTrain); }
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)
#include <iostream> struct event_stream_base { virtual ~event_stream_base() {} // this is the destructor; // there is a problem with the daniweb code formatter virtual void set_random_order(bool) = 0 ; } ; template< typename T > struct event_stream : event_stream_base { explicit event_stream( const T& v ) : member(v) {} virtual void set_random_order(bool) { std::cout << member << '\n' ; } T member ; // ... }; int main() { int a = 8 ; event_stream_base* pesb = 0 ; if( a > 5 ) pesb = new event_stream<double>(2.34) ; else pesb = new event_stream<int>(73) ; pesb->set_random_order(true) ; delete pesb ; }
Last edited by vijayan121; Jan 12th, 2008 at 3:55 pm.
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
> 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).
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
well, for the simple example you posted, you could write
but this may not be pragmatic in more complex cases (where the second if is not proximate to the first if-else)
C++ Syntax (Toggle Plain Text)
if(ARG::metrn==metrnSMD) { EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain); if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true); } else { EventStream<Lambda> evstrTrain(fileTrain, evlsTrain); if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true); }
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
well, for the simple example you posted, you could write
but this may not be pragmatic in more complex cases (where the second if is not proximate to the first if-else)C++ Syntax (Toggle Plain Text)
if(ARG::metrn==metrnSMD) { EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain); if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true); } else { EventStream<Lambda> evstrTrain(fileTrain, evlsTrain); if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true); }
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
> 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)
#include <iostream> #include <boost/any.hpp> #include <typeinfo> template< typename T > struct event_stream { explicit event_stream( const T& v ) : member(v) {} void set_random_order(bool) { std::cout << member << '\n' ; } T member ; // ... }; int main() { int a = 8 ; boost::any any ; if( a > 5 ) any = event_stream<double>(2.34) ; else any = event_stream<int>(73) ; // ... if( any.type() == typeid( event_stream<double> ) ) boost::any_cast< event_stream<double> >( any).set_random_order(true) ; else if( any.type() == typeid( event_stream<int> ) ) boost::any_cast< event_stream<int> >( any).set_random_order(true) ; }
Last edited by vijayan121; Jan 12th, 2008 at 5:21 pm.
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Displaying Average (C)
- Pointers (C++)
- Help Me Please (C++)
Other Threads in the C++ Forum
- Previous Thread: i dont know how to continue...
- Next Thread: c++ compiler for TI 89 calculator
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






