我正在使用具有模板声明和定义的库文件 Slist.h。库文件在 mypgm.c(cpp 文件)的构造函数中实例化。编译器抛出底部提到的错误。
列表.h
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;
}
我的pgm.c
class mypgm
{
public:
mypgm::mypgm()
{
ptr_dead_acc_list= new SList<String>; // ptr_dead_acc_list is of type SList<String>
}
};
字符串类详细信息供您参考
class String
{
private:
char *_text;
friend class String_Iterator;
public:
// ctors and dtor
explicit String ();
explicit String (char *);
explicit String (int );
String (String& );
~String();
/////////////////
// conversions:
/////////////////
// to C-like type char *
operator char *() {return _text;}
/////////////////
// overloads:
/////////////////
// assignment String = String
String &operator=(String &);
}
编译错误:
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]
我觉得在即时离子期间参数没有正确传递,我试图解决这个问题,但我的步骤是徒劳的。我是模板概念的新手。你能帮我解决这个问题吗?
感谢您查看这个