模板类错误的复制构造函数被调用

tun*_*2fs 3 c++ templates

为了避免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>&)
Run Code Online (Sandbox Code Playgroud)

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

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));
}
Run Code Online (Sandbox Code Playgroud)

这段代码有什么问题,为什么调用错误的拷贝构造函数?在没有模板的此类的以前版本中,这工作正常.

这里是相关类的标题.模板实现位于头文件中.

这里是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

};
Run Code Online (Sandbox Code Playgroud)

第二个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) {

}
Run Code Online (Sandbox Code Playgroud)

CB *_*ley 5

您的问题基本上是由该成员引起的:

std::auto_ptr<AlgorithmInterface<Weight> > _algorithm;
Run Code Online (Sandbox Code Playgroud)

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

这意味着当编译器为MPINode专门化生成复制构造函数时,它无法生成将const引用带到另一个的复制构造函数MPINode,它只能生成一个带有非const引用的复制构造函数.

在此初始化中,临时MPINode<WeightValue>无法绑定到生成的复制构造函数所需的非const引用参数.

MPINode<WeightValue> node = MPINode<WeightValue>(alg, nodeType, 
      tempNodeId, _nodeDistribution, _localNodes);
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题取决于您的设计.可能是提供使用const引用并正确克隆_algorithm成员的用户定义的复制构造函数是正确的方法.