Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy can't I perform a dynamic cast? Strategy pattern in C
    primarykey
    data
    text
    <p>I'm trying to implement the strategy pattern in C++ to make my code more flexible (and get to learn some OO programing). The dynamic cast showing up on the 3rd line in main() fails. Also I get some quite wierd error messages. Could you explain to me what I'm doing wrong? I'm not used to using design patterns in C++. </p> <p>The error message I get from the compiler:</p> <pre><code>/* /tmp/cc8b482g.o: In function `individual::setObjectiveFunction(int)': main.cpp:(.text+0x20b): undefined reference to `ObjectiveFunctionhyperBowl::ObjectiveFunctionhyperBowl()' /tmp/cc8b482g.o: In function `main': main.cpp:(.text+0x25e): undefined reference to `ObjectiveFunctionhyperBowl::ObjectiveFunctionhyperBowl()' main.cpp:(.text+0x344): undefined reference to `ObjectiveFunctionhyperBowl::~ObjectiveFunctionhyperBowl()' main.cpp:(.text+0x3ac): undefined reference to `ObjectiveFunctionhyperBowl::~ObjectiveFunctionhyperBowl()' */ </code></pre> <p>Edit: There was a correct suggestion from "6502" that I was using a class before it was declared (this is a trimmed version of the whole code)</p> <p>The constructor of ObjectiveFunctionhyperBowl are there, arent they? </p> <p>What am I missing, the declaration is now right above from where it is used and it still doesn't work...</p> <p>Thanx in advance again!</p> <p>And many thanx for the fast responses! - This is my first question in Stack Overflow, I promise I'll repay the deed helping other programers when I become a better programer!</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;random&gt; #include &lt;assert.h&gt; #include &lt;vector&gt; class individual; class objectiveFunctionStrategy; /* * This is my abstract class implementation for a strategy. */ class objectiveFunctionStrategy { public: objectiveFunctionStrategy(); virtual ~objectiveFunctionStrategy(); virtual float evaluate(individual)=0; }; /* * The class individual should contain strategy objects */ class individual{ private: std::vector&lt;float&gt; featureVector; objectiveFunctionStrategy * ofstrategy; unsigned int ndim; public: /*Constructors*/ individual(std::vector&lt;float&gt;); float getObjectiveFunction(); void setObjectiveFunction(int k); std::vector&lt;float&gt; getFeatureVector(); }; individual::individual(std::vector&lt;float&gt; fv){ for(unsigned int i=0;i&lt;fv.size() ; i++){ featureVector.push_back(fv[i]); } this -&gt; ndim = featureVector.size(); } std::vector&lt;float&gt; individual::getFeatureVector(){ return this-&gt;featureVector; } float individual::getObjectiveFunction(){ /* *Here I'm planning of passing a concrete strategy object to *make the objective functions interchangeable. * *So the calculation would be ofstrategy.evaluate() */ float Fc=0; if(false == false){ std::vector&lt;float&gt; vv = this-&gt;featureVector; Fc = ofstrategy-&gt;evaluate(vv); } return Fc; } /* * Now this one is a concrete strategy object class: */ class ObjectiveFunctionhyperBowl: public objectiveFunctionStrategy { public: /* * could have a reference to the individual. * might do things a bit more complicated. */ ObjectiveFunctionhyperBowl(); ~ObjectiveFunctionhyperBowl(); float evaluate(individual ind){ float Fc =0 ; std::vector&lt;float&gt; v = ind.getFeatureVector(); for(unsigned int i=0;i&lt;v.size();i++){ Fc += v[i]*v[i]; } return Fc; } }; void individual::setObjectiveFunction(int i){ /* * here the strategy is defined inside the class by an integer */ this-&gt;ofstrategy = new ObjectiveFunctionhyperBowl; } int main(){ std::vector&lt;float&gt; v; ObjectiveFunctionhyperBowl hb; objectiveFunctionStrategy* ofs; ofs = dynamic_cast&lt;objectiveFunctionStrategy*&gt; (&amp;hb); individual indiv(v); /* * Now the above still does not compile... * error message: * * * */ v.push_back(2.1); v.push_back(22.1); hb.evaluate(indiv); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload