Issue with a templatized class

This is a discussion on Issue with a templatized class within the C++ Programming forums, part of the General Programming Boards category; I recently tried cleaning up my code by separating it into smaller files and separating class member function declarations and ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Issue with a templatized class

    I recently tried cleaning up my code by separating it into smaller files and separating class member function declarations and definitions to get around issues relating to 'class has not yet been defined' type errors. In the case of one class, I have encountered errors which I assume are related to syntax, but I have spent the last couple of days trying to find a working alternative without luck, so I'm asking for advice here.

    In message_related.cpp

    Code:
    const unsigned int MSG_TURNEND = 1; // A message sent when a turn passes.
    const unsigned int MSG_POS = 2; // Increment counter, set boolean to true etc.
    const unsigned int MSG_NEG = 3; // Decrement counter, set boolean to false etc.
    
    template <class gen> class msg { // Might have an 'upstream' and 'downstream' version?
      unsigned int target; // The ID of the intended recipient of the message.
      unsigned int content; // The 'type' of the message - a touch message, a break message etc.
      gen ori; // Where to send 'return' messages.
    
    public:
    
      msg(unsigned int dest, unsigned int cont, gen or);
    
    };
    In message_related.hpp

    Code:
      msg <class gen> (unsigned int dest, unsigned int cont, gen or)
      {
        target = dest;
        content = cont;
        ori = or;
      }
    This is the (reduced for posting) setup I have at present, though I have tried others with varying error counts. As far as I can tell the <class gen> in the .hpp seems to be causing a "declaration terminated incorrectly" error, but removing that bit results in the same error message plus a "cannot use template specialisation without specifying specialisation parameter" type error. I have tried using the usual scope resolution (msg::msg) but still seem to get errors. Am I trying to do the impossible or just going about this the wrong way?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Templates often break the guideline of "definitions in a module, declarations in a header". The template code needs to be in the header.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Am I trying to do the impossible or just going about this the wrong way?
    You just don't know the correct syntax. What book are you using as a reference?

    First of all, the convention is to use .h files for class declarations, and .cpp files for class function definitions. In the olden days, when Wyatt Earp was programming his first games, they used .hpp files for declarations and .cpp files for definitions, which you have managed to get completely backwards. Also, the convention is to capitalize class names.

    As for template definitions, the powers that be actually created the 'typename' keyword so that you don't have to use the confusing 'class' in your template header, e.g. you can write:
    Code:
    template<typename T>
    class Msg
    {
    };
    Using T is the recommended convention in my book for the type, which is going to be a variable in a template.

    As for defining templated member functions, you have to list the template "specialization parameters" of which you only have one:
    Code:
    template<typename T>
    Msg<T>::Msg(unsigned int dest, unsigned int cont, gen or)
    {
    }
    In the Msg<T> part you are not allowed to use ''typename" or "class".
    Last edited by 7stud; 02-15-2006 at 10:07 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Yeah, and what Dave_Sinkula said. For a discussion of why, see here:

    Template class declarations
    Last edited by 7stud; 02-15-2006 at 10:05 PM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    24

    Thanks again

    I've gotten it to compile and not found any (new) problems.

    Templates often break the guideline of "definitions in a module, declarations in a header".
    I ran a check in the glossary on this site and couldn't find either of them. Can you please explain what exactly they are/represent?

    You just don't know the correct syntax. What book are you using as a reference?
    The only book I've got is C++: A Beginner's guide, Second Edition (which I gather not many people on the board think much of) which talks about templates but doesn't go as far as covering this.

    In the olden days, when Wyatt Earp was programming his first games
    Are they freeware now

    Also, the convention is to capitalize class names.
    I'm slowly getting into the habit.

    the powers that be actually created the 'typename' keyword so that you don't have to use the confusing 'class' in your template header
    According to my book, it's traditional to use class, which is the reason I use it.

    In the Msg<T> part you are not allowed to use ''typename" or "class".
    I almost got it right then. I think I tried something along the lines of msg<T>::msg<T>.

    Yeah, and what Dave_Sinkula said. For a discussion of why, see here:
    I'm going through it now.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This may better explain things (or some other FAQ on that page).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I ran a check in the glossary on this site and couldn't find either of them. Can you please explain what exactly they are/represent?
    I'm not sure what you are confused about, but normally you put class declarations in a .h file(i.e. a header file), and class function definitions in a .cpp file(which includes the .h file).

    The only book I've got is C++: A Beginner's guide, Second Edition (which I gather not many people on the board think much of) which talks about templates but doesn't go as far as covering this.
    I recommend it all the time. I think the books in the Beginner's Guide series are great. I'm surprised it talks about templates at all. It sounds like you might almost be finished with the book. What do you think of it?

    According to my book, it's traditional to use class, which is the reason I use it.
    Suit yourself. It doesn't matter.
    Last edited by 7stud; 02-16-2006 at 12:25 AM.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    I'm not sure what you are confused about, but normally you put class declarations in a .h file(i.e. a header file), and class function definitions in a .cpp file(which includes the .h file).
    I wasn't sure whether in the post

    Templates often break the guideline of "definitions in a module, declarations in a header". The template code needs to be in the header.
    'header' was referring to the file the declarations were in, the declaration of the class or its member functions, or something else. Since I'm used to thinking of 'modules' as a programming technique (something along the lines of separating the components (ie classes) so that changes to one won't affect the others) I didn't have a clue how to interpret it. Now, I assume that 'module' in this sense means a .cpp file, while 'header' means a .h or .hpp file (which I've learnt to consider as 'include' files, though I'm not sure that I got that from any particular source). Also, I suspect I was in need of a break (it took me a while to get a simple copy and paste job done) so my reasoning skills were probably a bit dull.

    I recommend it all the time. I think the books in the Beginner's Guide series are great. I'm surprised it talks about templates at all.
    It shows how to use template functions and classes, though when it discusses the classes, it deviates from its previous approach of defining class functions outside of the class which is why I was unfamiliar with the syntax.

    It sounds like you might almost be finished with the book. What do you think of it?
    I've read through it, but I wouldn't say I was finished with it (in particular, I know I need to work on streams and bitwise operators). I think it was pretty good, though I thought the learning curve was somewhat variable. That said, I haven't read any other C++ books so I can't really say whether this could have been avoided.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 02:52 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21