gpt4 book ai didi

c++ - 模板类调用了错误的复制构造函数

转载 作者:太空狗 更新时间:2023-10-29 20:05:52 26 4
gpt4 key购买 nike

解决方案

为了避免 std::auto_ptr 的问题,可以切换到 boost::shard_ptr 或 C++11 std::shared_ptr。

我得到一个错误,在我的模板类中调用了错误的复制构造函数:

MPINetworkCode.hpp: error: no matching function for call to 
MPILib::MPINode<double>::MPINode(MPILib::MPINode<double>)

MPINode.hpp: note: candidate is:
MPILib::MPINode<double>::MPINode(MPILib::MPINode<double>&)

以下是导致此错误的代码行。

int MPINetwork<WeightValue>::AddNode(const AlgorithmInterface<WeightValue>& alg,
NodeType nodeType) {

MPINode<WeightValue> node = MPINode<WeightValue>(alg, nodeType,
tempNodeId, _nodeDistribution, _localNodes);
_localNodes.insert(std::make_pair(tempNodeId, node));
}

这段代码有什么问题,为什么调用了错误的复制构造函数?在没有模板的此类的先前版本中,这工作正常。

这里是相关类的标题。模板实现在头文件中。

这里是 MPINetwork:

template <class WeightValue>
class MPINetwork: private boost::noncopyable {

public:

explicit MPINetwork();

~MPINetwork();

/**
* Adds a new node to the network
* @param alg The Algorithm of the actual node
* @param nodeType The Type of the Node
* @return returns the NodeId of the generated node
*/
int AddNode(const AlgorithmInterface<WeightValue>& alg, NodeType nodeType);

//lot of code

};

第二个 MPINode,应该调用默认的复制构造函数:

template <class Weight>
class MPINode {
public:
/**
* Constructor
* @param algorithm Algorithm the algorithm the node should contain
* @param nodeType NodeType the type of the node
* @param nodeId NodeId the id of the node
* @param nodeDistribution The Node Distribution.
* @param localNode The local nodes of this processor
*/
explicit MPINode(const AlgorithmInterface<Weight>& algorithm, NodeType nodeType,
NodeId nodeId,
const boost::shared_ptr<utilities::NodeDistributionInterface>& nodeDistribution,
const std::map<NodeId, MPINode<Weight> >& localNode);

virtual ~MPINode();

Time Evolve(Time time);

void ConfigureSimulationRun(const SimulationRunParameter& simParam);

void addPrecursor(NodeId nodeId, const Weight& weight);

void addSuccessor(NodeId nodeId);

NodeState getState() const;

void setState(NodeState state);

void receiveData();

void sendOwnState();

private:

void waitAll();

std::vector<NodeId> _precursors;

std::vector<Weight> _weights;

std::vector<NodeId> _successors;

std::auto_ptr<AlgorithmInterface<Weight> > _algorithm;

NodeType _nodeType;

NodeId _nodeId;

const std::map<NodeId, MPINode>& _refLocalNodes;

boost::shared_ptr<utilities::NodeDistributionInterface> _nodeDistribution;

NodeState _state;

std::vector<NodeState> _precursorStates;

std::vector<boost::mpi::request> _mpiStatus;
};

template<class Weight>
MPINode<Weight>::MPINode(const AlgorithmInterface<Weight>& algorithm, NodeType nodeType,
NodeId nodeId,
const boost::shared_ptr<utilities::NodeDistributionInterface>& nodeDistribution,
const std::map<NodeId, MPINode>& localNode) :
_algorithm(algorithm.Clone()), _nodeType(nodeType), _nodeId(nodeId), _nodeDistribution(
nodeDistribution), _refLocalNodes(localNode) {

}

最佳答案

您的问题本质上是由该成员(member)引起的:

std::auto_ptr<AlgorithmInterface<Weight> > _algorithm;

std::auto_ptr的复制构造函数并没有真正复制,它转移了所有权。因此,它需要对其参数进行非 const 引用,而不是 const 引用。

这意味着当编译器开始为 MPINode 生成复制构造函数时特化它不能生成一个复制构造函数,该复制构造函数采用对另一个的 const 引用 MPINode ,它只能生成一个采用非 const 引用的对象。

在此初始化中,临时 MPINode<WeightValue>不能绑定(bind)到生成的复制构造函数需要的非常量引用参数。

MPINode<WeightValue> node = MPINode<WeightValue>(alg, nodeType, 
tempNodeId, _nodeDistribution, _localNodes);

如何解决这个问题取决于您的设计。可能是提供一个用户定义的复制构造函数,它采用常量引用并正确克隆 _algorithm成员是正确的做法。

关于c++ - 模板类调用了错误的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10894058/

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