Thread: C++ template error I think?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    C++ template error I think?

    I'm new to C++ but we're supposed to implement a provided template for a linked list. The error I'm getting says:
    Code:
    ..\src\dlist.cc:3: error: expected constructor, destructor, or type conversion before '<' token
    I get this error for my constructor methods in my .cc file. Without giving too much away, here is what my code looks like:
    Code:
    //Constructor
    template <typename T>
    Dlist<T>::Dlist()
    {
    	makeEmpty();
    }
    
    //Copy constructor
    template <typename T>
    Dlist<T>::Dlist(const Dlist &l)
    {
    	makeEmpty();
    	copyAll(l);
    }
    
    //Destructor
    template <typename T>
    Dlist<T>::~Dlist()
    {
    	makeEmpty();
    	removeAll();
    }
    
    //Equals operator
    template <typename T>
    Dlist<T>& Dlist<T>::operator=(const Dlist &l)
    {
    	if(this!=&l)
    		copyAll(l);
    	return *this;
    }
    //more methods
    I'm certain that the header is correct because it was provided. I think I'm just doing something stupidly wrong.

    PS. The other methods all say:
    Code:
    error: expected initializer before '<' token
    Last edited by jcafaro10; 01-13-2009 at 11:53 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Um. Are you including a file that defines DList? If it's not a known class name, then you can't use it before the :: namespace qualifier.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I'm not sure what you mean. My file dlist.cc doesn't have any includes. It starts with the code I posted. There is no code or anything above what I posted, only some methods afterwards. The only other file is dlist.h which includes dlist.cc

    dlist.h:
    Code:
    #ifndef __DLIST_H__
    #define __DLIST_H__
    
    class emptyList
    {
        // OVERVIEW: an exception class
    };
    
    template <typename T>
    class Dlist
    {
        // OVERVIEW: contains a double-ended list of Objects
    
     public:
    
        // Operational methods
    
        bool isEmpty();
        // EFFECTS: returns true if list is empy, false otherwise
    
        void insertFront(T *o);
        // MODIFIES this
        // EFFECTS inserts o at the front of the list
    
        void insertBack(T *o);
        // MODIFIES this
        // EFFECTS inserts o at the back of the list
    
        T *removeFront();
        // MODIFIES this
        // EFFECTS removes and returns first object from non-empty list
        //         throws an instance of emptyList if empty
    
        T *removeBack();
        // MODIFIES this
        // EFFECTS removes and returns last object from non-empty list
        //         throws an instance of emptyList if empty
    
        // Maintenance methods
        Dlist();                                   // ctor
        Dlist(const Dlist &l);                     // copy ctor
        Dlist &operator=(const Dlist &l);          // assignment
        ~Dlist();                                  // dtor
    
     private:
        // A private type
        struct node
    	{
    		node   *next;
    		node   *prev;
    		T      *o;
        };
    
        node   *first; // The pointer to the 1st node (NULL if none)
        node   *last;  // The pointer to the 2nd node (NULL if none)
    
        // Utility methods
    
        void makeEmpty();
        // EFFECT: called by constructors/operator= to establish empty
        // list invariant
    
        void removeAll();
        // EFFECT: called by destructor/operator= to remove and destroy
        // all list elements
    
        void copyAll(const Dlist &l);
        // EFFECT: called by copy constructor/operator= to copy elements
        // from a source instance l to this instance
    };
    
    /*
    Note: this is here *only* because the gnu compiler needs to see the
    "templatized" versions of your methods.  This is the *only* instance
    in which it is acceptable to #include a cc file.
    */
    
    #include "dlist.cc"
    
    #endif /* __DLIST_H__ */

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, what are you trying to compile then? There's nothing in file.h or in file.cc that's compilable, and file.c cannot be used as-is anyway since the definitions in dlist.h must precede it.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    So basically I need a file that contains main to get this to work. Does it look right otherwise?
    Last edited by jcafaro10; 01-13-2009 at 10:35 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to actually write a main, yes. I have no idea whether you're doing anything right. At the very least I'm pretty sure that your operator= needs to take a DList<T> rather than a just plain DList.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look at the first line in your .cc file. You intend it to be a comment, but have left out one character so it is not.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Sorry, that's not actually in my code, just got messed up in copying over.

    I made a simple main class that just returns 0 but it has no effect on the errors. I'm still getting the errors that I mentioned.
    Code:
    #include <iostream>
    #include "dlist.h"
    
    using namespace std;
    
    int main()
    {
        return 0;
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How did you compile it? If you compile it correctly, as in
    Code:
    $ g++ -Wall jcafaro.cpp
    then nothing happens (as in no errors). Note that you still must not compile your dlist.cc file, except via dlist.h's include statement; that means it must not appear on your command line at all. If you are using an IDE, then you're going to have to do something fancy to get around the fact that the IDE thinks it knows better when it builds your makefile, by making your own makefile or giving dlist.cc an extension it does not recognize.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Now that you've fixed the defective comment on the first line, your code compiles without problems.

    My guess is that the code you're showing us differs from what you're feeding to the compiler in some way.

    If all you've done is copy and paste the files into your posts, then the problem is with something that doesn't survive the copy/paste process intact. For example, whitespace, non-graphical characters.

    I have seen problems similar to this before, and they were solved by adding a blank line at the end of your header file and at the end of all your source files. This is a problem for some unix compilers (or for files that have been fed repeatedly through programs like dos2unix and unix2dos) as they typically ignore or discard a line that is terminated by an end-of-file marker rather than an end-of-line marker.

    But that's just a guess .......

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    //Constructor
    template <typename T>
    Dlist<T>::Dlist()
    {
    	makeEmpty();
    }
    
    //Copy constructor
    template <typename T>
    Dlist<T>::Dlist(const Dlist<T>& l)
    {
    	makeEmpty();
    	copyAll(l);
    }
    
    //Destructor
    template <typename T>
    Dlist<T>::~Dlist()
    {
    	makeEmpty();
    	removeAll();
    }
    
    //Equals operator
    template <typename T>
    Dlist<T>& Dlist<T>::operator=(const Dlist<T>& l)
    {
    	if(this!=&l)
    		copyAll(l);
    	return *this;
    }
    I don't really have a compiler to test with at the moment...
    But I am pretty sure that you cannot refer to an unspecialized type outside the definition itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The parameter list of out-of-line member definitions is in the class scope, so plain DList should work and refer to the current specialization of the template.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I'm using Eclipse and Visual Studio. Both give approximately the same errors. Eclipse shows me what the build command is.
    Code:
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\dlist.o ..\src\dlist.cc
    I suppose that's what you said not to do. What am I supposed to get it to build?

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I figured it out. In eclipse you can exclude a file from build. I excluded the dlist.cc file from the build and it built the project correctly and even gave the correct output for a simple test program. Thanks for the help guys. How do I know when I can and when I can't include something in the build?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose it's not possible to rename dlist.cc? That would be the easy way, to give it an extenstion that eclipse or VS doesn't recognize so it won't try to compile it. I don't know how to customize make in Eclipse. In Visual Studio (at least VS2005) you can right-click on the file in the Solution Explorer and select "Exclude from Build".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM