gpt4 book ai didi

c++ - 模板和 g++ 4.7

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:02 25 4
gpt4 key购买 nike

我有这个代码

namespace MSL{

template <typename T> class TListNode;
template <typename T> class TList;
...

template <typename T> 
int TList<T>::add(T v) {
TListNode<T> *pn;
pn = new TListNode<T>;
...

class TMergeNode {
public:

inline TMergeNode(int cluster1=-1, int cluster2=-1, TCMData mergeVal=0);
inline TMergeNode(TMergeNode &b); // copy constructor
...

它在旧版本的 g++ 上编译正常,但现在在 4.7 版中出现以下错误:

./msl/MSL_List_Template.h: In instantiation of ‘int MSL::TList<T>::add(T) [with T = TMergeNode]’:
clustermerges.cpp:282:33: required from here
./msl/MSL_List_Template.h:616:23: error: no matching function for call to ‘TMergeNode::TMergeNode(TMergeNode)’
./msl/MSL_List_Template.h:616:23: note: candidates are:
In file included from main.cpp:78:0:
clustermerges.cpp:70:8: note: TMergeNode::TMergeNode(TMergeNode&)
clustermerges.cpp:70:8: note: no known conversion for argument 1 from ‘TMergeNode’ to ‘TMergeNode&’
clustermerges.cpp:68:8: note: TMergeNode::TMergeNode(int, int, MSL::TCMData)
clustermerges.cpp:68:8: note: no known conversion for argument 1 from ‘TMergeNode’ to ‘int’

任何想法将不胜感激

最佳答案

在您的代码中,您试图将一个临时变量绑定(bind)到一个非常量引用。这是不允许的。

你的拷贝构造函数的正确签名应该是:

class TMergeNode {
public:
inline TMergeNode(const TMergeNode &b); // copy constructor
// ^^^^^

关于c++ - 模板和 g++ 4.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749350/

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