我正在尝试在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的构造函数在那里,不是吗?
我错过了什么,声明现在就在它的使用位置上方,它仍然不起作用......
再次提前Thanx!
快速回复还有很多次! - 这是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);
}
答案 0 :(得分:4)
您的代码正在使用
new ObjectiveFunctionhyperBowl;
在声明课程ObjectiveFunctionhyperBowl
之前;在这种情况下,C ++不进行预测,因此您必须在使用它之前声明该类。
在individual::setObjectiveFunction
方法实现之前移动类声明应该足够了。一般来说,如果可能的话,最好在include .h
文件中包含所有声明(和声明),并在.cpp
单独文件中实现。