我正在尝试在 C++ 中实现策略模式,以使我的代码更加灵活(并开始学习一些 OO 编程)。main() 中第 3 行显示的动态转换失败。我还收到一些非常奇怪的错误消息。你能向我解释我做错了什么吗?我不习惯在 C++ 中使用设计模式。
我从编译器得到的错误信息:
/*
/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()'
*/
编辑:“6502”有一个正确的建议,即我在声明之前使用了一个类(这是整个代码的修剪版本)
ObjectiveFunctionhyperBowl 的构造函数在那里,不是吗?
我错过了什么,声明现在就在使用它的上方,但它仍然不起作用......
再次提前谢谢!
感谢您的快速响应!- 这是我在 Stack Overflow 上的第一个问题,我保证当我成为更好的程序员时,我会回报帮助其他程序员的行为!
#include <iostream>
#include <sstream>
#include <random>
#include <assert.h>
#include <vector>
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<float> featureVector;
objectiveFunctionStrategy * ofstrategy;
unsigned int ndim;
public:
/*Constructors*/
individual(std::vector<float>);
float getObjectiveFunction();
void setObjectiveFunction(int k);
std::vector<float> getFeatureVector();
};
individual::individual(std::vector<float> fv){
for(unsigned int i=0;i<fv.size() ; i++){
featureVector.push_back(fv[i]);
}
this -> ndim = featureVector.size();
}
std::vector<float> individual::getFeatureVector(){
return this->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<float> vv = this->featureVector;
Fc = ofstrategy->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<float> v = ind.getFeatureVector();
for(unsigned int i=0;i<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->ofstrategy = new ObjectiveFunctionhyperBowl;
}
int main(){
std::vector<float> v;
ObjectiveFunctionhyperBowl hb;
objectiveFunctionStrategy* ofs;
ofs = dynamic_cast<objectiveFunctionStrategy*> (&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);
}