C++ 模板实例化问题
我正在使用具有模板声明和定义的库文件Slist.h (cpp文件)。编译器抛出一个错误,该错误在底部提到 Slist.hC++ 模板实例化问题,c++,templates,compiler-errors,instantiation,C++,Templates,Compiler Errors,Instantiation,我正在使用具有模板声明和定义的库文件Slist.h (cpp文件)。编译器抛出一个错误,该错误在底部提到 Slist.h template <class Object> class SList; // fwd declaration. template <class Object> class SListItr; // fwd declaration. template <class Object> class SListNode { SLi
template <class Object>
class SList; // fwd declaration.
template <class Object>
class SListItr; // fwd declaration.
template <class Object>
class SListNode
{
SListNode( const Object &theElement = Object( ),
SListNode *n = NULL )
: element( theElement ), next( n ) { }
Object element;
SListNode *next;
friend class SList<Object>;
friend class SListItr<Object>;
};
template <class Object>
class SList
{
public:
SList( );
SList( const SList &rhs );
~SList( );
int isEmpty( ) const;
void makeEmpty( );
SListItr<Object> zeroth( ) const;
SListItr<Object> first( ) const;
const SList & operator=( SList &rhs );
int IsType() {return LinkedListType;};
private:
SListNode<Object> *header;
Object *ptr_Object;
};
//SList definition
template <class Object>
SList<Object>::SList( )
{
ptr_Object = new Object();
header = new SListNode<Object>;
}
// Copy constructor.
template <class Object>
SList<Object>::SList( const SList<Object> &rhs )
{
header = new SListNode<Object>;
*this = rhs;
}
// Destructor.
template <class Object>
SList<Object>::~SList( )
{
makeEmpty( );
delete ptr_Object;
delete header;
}
编译错误:
SList.h: In constructor 'SList<Object>::SList() [with Object = String]':
mypgm.c:131: instantiated from here
SList.h:85: error: no matching function for call to 'String::String(String)'
String.h:27: note: candidates are: String::String(String&)
SList.h:41: error: in passing argument 1 of 'SListNode<Object>::SListNode(const Object&, SListNode<Object>*) [with Object = String]'
SList.h: In constructor 'SListNode<Object>::SListNode(const Object&, SListNode<Object>*) [with Object = String]':
SList.h:85: instantiated from 'SList<Object>::SList() [with Object = String]'
mypgm.c:131: instantiated from here
SList.h:42: error: passing 'const String' as 'this' argument of 'String::operator char*()' discards qualifiers
make: *** [all]
SList.h:在构造函数“SList::SList()[with Object=String]”中:
mypgm.c:131:从此处实例化
SList.h:85:错误:调用“String::String(String)”时没有匹配的函数
String.h:27:注:候选项为:String::String(String&)
SList.h:41:错误:在传递'SListNode::SListNode(const Object&,SListNode*)[with Object=String]的参数1中
h:在构造函数“SListNode::SListNode(const Object&,SListNode*)[with Object=String]”中:
SList.h:85:从'SList::SList()[with Object=String]实例化'
mypgm.c:131:从此处实例化
SList.h:42:错误:将'const String'作为'String::operator char*()'的'this'参数传递将丢弃限定符
制作:**[全部]
我觉得在即时更新过程中没有正确地传递参数,我试图解决这个问题,但我的步骤是徒劳的。
我不熟悉模板的概念。你能帮我解决这个问题吗
感谢您查看此您的问题没有任何问题。
字符串
的复制构造函数应该接受对常量的引用。与此相反:
String (String& );
签名应如下所示:
String(String const&);
SListNode
正在调用常量对象上的String
构造函数,编译器会抱怨,因为您的复制构造函数接受了非常量引用:
SListNode( const Object &theElement = Object( ),
SListNode *n = NULL )
: element( theElement ), // ERROR: theElement is a reference to const!
next( n ) { }
另外请注意,尽管您的问题源于模板的实例化,但它与模板并没有特别的关系。在非模板代码中执行相同的操作将导致相同的错误。您的问题没有任何问题。
字符串的复制构造函数应接受对常量的引用。与此相反:
String (String& );
签名应如下所示:
String(String const&);
SListNode
正在调用常量对象上的String
构造函数,编译器会抱怨,因为您的复制构造函数接受了非常量引用:
SListNode( const Object &theElement = Object( ),
SListNode *n = NULL )
: element( theElement ), // ERROR: theElement is a reference to const!
next( n ) { }
另外请注意,尽管您的问题源于模板的实例化,但它与模板并没有特别的关系。在非模板代码中执行相同的操作将导致相同的错误。该错误与模板无关,只是在实例化模板时碰巧弹出。您的类String
是不寻常的,因为它不是“可复制的”,这会阻止以许多正常方式使用它
它应该有一个构造函数
String(const String&);
而不是
String(String&);
该错误与模板无关,只是在实例化模板时碰巧弹出。您的类String
是不寻常的,因为它不是“可复制的”,这会阻止以许多正常方式使用它
它应该有一个构造函数
String(const String&);
而不是
String(String&);
事实上,我想进一步澄清,这个问题与模板无关。@RyanWitmer:是的,这是我做的explicit@AndyProwl:非常感谢Andy的快速响应。我会尝试让您知道。我还有一个问题,我正在调用下面的参数化构造函数(header=new-SListNode;)SListNode(const-Object&theElement=Object(),SListNode*n=NULL):元素(theElement),下一个(n){}请参考我的question@user2040497:问题是什么?在上面的代码中,我正在调用下面的参数化构造函数(header=new-SListNode;)构造函数:-SListNode(const-Object&theElement=Object(),SListNode*n=NULL):元素(theElement),下一个(n){请参考我问题中的列表定义代码。1.调用模板的参数化构造函数是否正确?2.在实例化过程中是否需要传递任何参数?事实上,我想进一步澄清这个问题与模板无关。@RyanWitmer:是的,我这样做了explicit@Andy非常感谢安迪的提问ck响应。我将尝试让您知道。我还有一个问题,我正在调用下面的参数化构造函数(header=new-SListNode;)SListNode(const-Object&theElement=Object(),SListNode*n=NULL):element(theElement),next(n){请参考我的手册中的SList定义代码question@user2040497:问题是什么?在上面的代码中,我正在调用下面的参数化构造函数(header=new-SListNode;)构造函数:-SListNode(const-Object&theElement=Object(),SListNode*n=NULL):element(theElement),next(n){}请参考我问题中的列表定义代码。1.调用模板的参数化构造函数是否正确?2.实例化期间是否需要传递任何参数?