gpt4 book ai didi

c++ - 为什么我不能执行动态转换? C 中的策略模式

转载 作者:太空狗 更新时间:2023-10-29 21:01:01 25 4
gpt4 key购买 nike

我正尝试在 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);
}

最佳答案

您的代码正在使用

new ObjectiveFunctionhyperBowl;

在声明类 ObjectiveFunctionhyperBowl 之前;在这种情况下,C++ 不进行前瞻,因此您必须在使用前声明该类。

将类声明移到 individual::setObjectiveFunction 方法实现之前就足够了。一般来说,如果可能的话,最好在包含 .h 文件中包含所有声明(并且仅声明),并在 .cpp 单独文件中实现。

关于c++ - 为什么我不能执行动态转换? C 中的策略模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com