简体   繁体   中英

Templates and g++ 4.7

I have this code

namespace MSL{

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

and

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

and

class TMergeNode {
 public:

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

it compiled OK with older versions of g++, but now with version 4.7 I get the following errors:

./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’

Any idea would be appreciated

In your code you are trying to bind a temporary to a non-const reference. This is not allowed.

The correct signature of your copy constructor would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM