有时在VS2012下会出现链接错误,有时不会

时间:2017-09-06 12:58:01

标签: c++ visual-studio compilation

今天我在VS 2012下遇到了一个奇怪的链接错误。当我在编码后尝试构建我的解决方案时出现了链接错误(LNK2001)。当我通过注释添加的行取消更改时,项目构建。经过一些尝试,我得到一个奇怪的事情。 (下一行描述了我按实际顺序执行的操作)

当我用所有行构建项目时,我得到了LNK2001。

当我评论GeneticNetworkRealisation的第一个构造函数时 - 我得到了LNK2001。

当我评论GeneticNetwork的第二个构造函数时 - 错误消失。

但是当我将这一行返回代码时 - 仍然没有错误。

因此,这意味着两个相同的代码具有不同的链接成功。有关如何解决此问题的任何想法?简化的代码和错误:

NeuralNetwork.h

#pragma once

#include "Neuron.h"
#include "NeuralLayer.h"

struct Topology {
    // some variables
};

class NeuralNetwork {

public:

    NeuralNetwork(const Topology & topology);
    NeuralNetwork(const NeuralNetwork & nn) : layers(nn.layers) {}

    virtual ~NeuralNetwork() {}

protected:

    std::vector<NeuralLayer> layers;

};

GeneticNetwork.h

#pragma once

#include "NeuralNetwork.h"

class GeneticNetwork : public NeuralNetwork {

public:

    GeneticNetwork(const Topology & topology) : NeuralNetwork(topology) {}
    GeneticNetwork(const GeneticNetwork & gnn) : NeuralNetwork(gnn) {}

    virtual std::vector<double> getNetworkGeneticCode() const = 0;
    virtual void mutate() = 0;
    virtual NeuralNetwork crossingOver(const NeuralNetwork & nn) const = 0;

    virtual ~GeneticNetwork() {}
};

GeneticNetworkRealisation.h

#pragma once

#include "GeneticNetwork.h"
#include "URNG.h"

struct MutationParameters {
    // some parameters
};

const MutationParameters DEFAULT_MUTATION_PARAMS = {/*...*/};

class GeneticNetworkRealisation : public GeneticNetwork {

public:

    GeneticNetworkRealisation(const GeneticNetworkRealisation & gnn) :
            GeneticNetwork(gnn), random(gnn.random), mutationParams(gnn.mutationParams) {}

    GeneticNetworkRealisation(const Topology & topology, URNG urng = URNG(),
            const MutationParameters & mutationParameters = DEFAULT_MUTATION_PARAMS) :
            GeneticNetwork(topology), random(urng), mutationParams(mutationParameters) {}

    virtual std::vector<double> getNetworkGeneticCode() const override;
    virtual void mutate() override;
    virtual NeuralNetwork crossingOver(const NeuralNetwork & nn) const override;

    virtual ~GeneticNetworkRealisation() {}

protected:

    URNG & random;
    MutationParameters mutationParams;

};

GeneticNetworkRealisation.cpp

#include "GeneticNetworkRealisation.h"

void GeneticNetworkRealisation::mutate() {
    // code
}

std::vector<double> GeneticNetworkRealisation::getNetworkGeneticCode() const {
    // code
}

NeuralNetwork GeneticNetworkRealisation::crossingOver(const NeuralNetwork & nn) const {
    // code
}

错误:

1>main.obj : error LNK2001: unresolved external symbol ""public: virtual class std::vector<double,class std::allocator<double> > __thiscall GeneticNetworkRealisation::getNetworkGeneticCode(void)const " (?getNetworkGeneticCode@GeneticNetworkRealisation@@UBE?AV?$vector@NV?$allocator@N@std@@@std@@XZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual void __thiscall GeneticNetworkRealisation::mutate(void)" (?mutate@GeneticNetworkRealisation@@UAEXXZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual class NeuralNetwork __thiscall GeneticNetworkRealisation::crossingOver(class NeuralNetwork const &)const " (?crossingOver@GeneticNetworkRealisation@@UBE?AVNeuralNetwork@@ABV2@@Z)"

0 个答案:

没有答案