我認(rèn)為我對(duì)物體是如何構(gòu)造的有一個(gè)根本性的誤解。
我有一些代碼目前正在使用new
構(gòu)造對(duì)象,我被告知這是一種糟糕的做法。所以我想我可以去掉new
關(guān)鍵字,用實(shí)際對(duì)象替換指針,然后刪除delete
語(yǔ)句。不幸的是,當(dāng)我刪除新語(yǔ)句時(shí),得到error: no viable overloaded '='
。我不明白為什么。
我想做的對(duì)象是一個(gè)LightWeightNeuralNetwork
;見(jiàn)https://github.com/lwtnn/lwtnn/blob/master/include/lwtnn/LightweightNeuralNetwork.hh
編輯:UnholySheep在評(píng)論中指出,LightweightNeuralNetwork
沒(méi)有默認(rèn)構(gòu)造函數(shù),這就是問(wèn)題的根源(當(dāng)類(lèi)TNeuralNetworkLWTNN
初始化時(shí),它不知何故需要所有成員對(duì)象的默認(rèn)構(gòu)造函數(shù)?)。不幸的是,此處作為dupe提出的問(wèn)題解決方案將不起作用,因?yàn)樵摻鉀Q方案需要在初始值設(shè)定項(xiàng)列表中創(chuàng)建問(wèn)題對(duì)象。LightweightNeuralNetwork
需要打開(kāi)一個(gè)文件才能創(chuàng)建。
我的代碼通常是作為更大框架的一部分編譯的,但這里有一個(gè)玩具模型來(lái)演示相關(guān)的位。
頭文件;
#ifndef VNEURALNETWORKLWTNN_H
#define VNEURALNETWORKLWTNN_H
#include <iostream>
// Becuase we have a field of type LightweightNeuralNetwork
#include "lwtnn/LightweightNeuralNetwork.hh"
class TNeuralNetworkLWTNN {
public:
// same as for lwtnn
typedef std::map<std::string, double> NetworkInputs;
typedef std::map<std::string, double> NetworkOutputs;
// constructor and destructor
explicit TNeuralNetworkLWTNN(std::string inputFile);
~TNeuralNetworkLWTNN();
// business end
NetworkOutputs compute(NetworkInputs inputs);
private:
// !!Change this to not be a pointer!!
lwt::LightweightNeuralNetwork * lwtnn_neural;
};
#endif
和一個(gè)班級(jí);
#include "TNeuralNetworkLWTNN.h"
#include <fstream>
#include <sstream>
// LWTNN
#include "lwtnn/LightweightNeuralNetwork.hh"
#include "lwtnn/parse_json.hh"
TNeuralNetworkLWTNN::TNeuralNetworkLWTNN(std::string inputFile) {
// The input file is read into a stringstream
std::ifstream input(inputFile);
std::stringstream sin;
sin << input.rdbuf();
input.close();
// build the graph
lwt::JSONConfig config = lwt::parse_json(sin);
// !! I don't want to be using new !!
// !! Change this to just use '=' !!
// !! error: no viable overloaded '=' !!
lwtnn_neural = new lwt::LightweightNeuralNetwork(config.inputs, config.layers,
config.outputs);
};
// I'd rather not need any code here
TNeuralNetworkLWTNN::~TNeuralNetworkLWTNN(){
delete lwtnn_neural;
};
TNeuralNetworkLWTNN::NetworkOutputs
TNeuralNetworkLWTNN::compute(TNeuralNetworkLWTNN::NetworkInputs inputs) {
// !! Change this to use . not -> !!
TNeuralNetworkLWTNN::NetworkOutputs outputs =
TNeuralNetworkLWTNN::lwtnn_neural->compute(inputs);
return outputs;
};
很抱歉,沒(méi)有生成您可以編譯的內(nèi)容,在我正在編寫(xiě)的框架之外,我無(wú)法訪(fǎng)問(wèn)lwtnn庫(kù)。我希望我的錯(cuò)誤是很基本的。
完整的錯(cuò)誤消息(修改了一些路徑);
/path/to/repo/TNeuralNetworkLWTNN.cxx:9:22: error: constructor for 'TNeuralNetworkLWTNN' must explicitly initialize the member 'lwtnn_neural' which does not have a default constructor
TNeuralNetworkLWTNN::TNeuralNetworkLWTNN(std::string inputFile)
^
/path/to/repo/TNeuralNetworkLWTNN.h:36:34: note: member is declared here
lwt::LightweightNeuralNetwork lwtnn_neural;
^
/path/to/library/lwtnn/LightweightNeuralNetwork.hh:41:9: note: 'lwt::LightweightNeuralNetwork' declared here
class LightweightNeuralNetwork
^
/path/to/repo/TNeuralNetworkLWTNN.cxx:18:16: error: no viable overloaded '='
lwtnn_neural = lwt::LightweightNeuralNetwork(config.inputs, config.layers,
~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/path/to/releases/lwtnn/2.11.1-2ee64/x86_64-centos7-gcc11-opt/include/lwtnn/LightweightNeuralNetwork.hh:52:31: note: candidate function not viable: expects an lvalue for 1st argument
LightweightNeuralNetwork& operator=(LightweightNeuralNetwork&) = delete;
^
2 errors generated.
make[2]: *** [CMakeFiles/GenericTest.dir/TNeuralNetworkLWTNN.cxx.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/GenericTest.dir/all] Error 2
make: *** [all] Error 2
問(wèn)題是
lwt::LightweightNeuralNetwork
是不可移動(dòng)的,正如@n.1.8e9-where's-my-sharem.所指出的,這似乎是庫(kù)中的一個(gè)bug,因?yàn)樗坪鯖](méi)有任何理由不可移動(dòng)。如果您不想或由于某種原因無(wú)法自己對(duì)庫(kù)進(jìn)行更改(并且等待它被修復(fù)是一種不可接受的延遲),那么您可以簡(jiǎn)單地將
lwt::LightweightNeuralNetwork * lwtnn_neural;
替換為std::unique_ptr<lwt::LightweightNeuralNetwork> lwtnn_neural;
,并在構(gòu)造函數(shù)中將lwtnn_neural = new lwt::LightweightNeuralNetwork(config.inputs, config.layers, config.outputs);
替換成lwtnn_neural = std::make_unique<lwt::LightweightNeuralNetwork>(config.inputs, config.layers, config.outputs);
然而,添加移動(dòng)構(gòu)造和分配非常簡(jiǎn)單,因此您可以考慮自己進(jìn)行,例如:
然后你可以有一個(gè)non-pointer成員和一個(gè)額外的函數(shù)來(lái)初始化成員初始值設(shè)定項(xiàng)列表中的
lwt::LightweightNeuralNetwork
,例如: