解决方案
为了避免 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) {
}