user2040497
user2040497

Reputation: 15

Template instantiation Issues

I am using library file Slist.h which has template declaration and definition.The library file is instantiated in the construtor of mypgm.c(cpp file).The compiler throws an error which is mentioned at the bottom.

Slist.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;
}

mypgm.c

class mypgm
{
public:
    mypgm::mypgm()
    {
    ptr_dead_acc_list= new SList<String>; // ptr_dead_acc_list is of type SList<String>
    }
 };

String class details for your reference

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 &);
}

Compilation Error:

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]

I feel that the arguments are not passed correctly during instant ion,i have tried to resolve this ,but my steps are in vain. i am new to template concept.could you please help me to resolve this issue.

Thanks for looking into this

Upvotes: 0

Views: 117

Answers (2)

aschepler
aschepler

Reputation: 72463

That error has nothing to do with the templates, it just happened to pop up while instantiating a template. Your class String is unusual because it is not "CopyConstructible", which prevents using it in many of the normal ways.

It should probably have a constructor

String(const String&);

instead of

String(String&);

Upvotes: 0

Andy Prowl
Andy Prowl

Reputation: 126552

Your problem has nothing doThe copy constructor of String should accept a reference to constant. Instead of this:

String (String& );

The signature should look like this:

String(String const&);

SListNode is invoking the constructor of String on a constant object, and the compiler complains because your copy-constructor accepts a non-constant reference:

SListNode( const Object &theElement = Object( ),
           SListNode *n = NULL )
  : element( theElement ), // ERROR: theElement is a reference to const!
    next( n ) { }

Also notice, that although your problem stems from the instantiation of a template, it is not specifically related to templates. Doing the same thing in non-template code would result in the same error.

Upvotes: 4

Related Questions