3

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

1 回答 1

4

您的代码正在使用

new ObjectiveFunctionhyperBowl;

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

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

于 2013-11-12T23:40:41.997 回答