Primitive Templated Class

This is a discussion on Primitive Templated Class within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'll get straight to the point; I've got a simple templated class, with some template specialization thrown in. ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    Primitive Templated Class

    Hey all,

    I'll get straight to the point; I've got a simple templated class, with some template specialization thrown in. The source is included. The problem is, I'm getting an "undefined reference" when I try to compile. I have no idea what I'm doing wrong, I've worked with templates before and never encountered this problem. From the error I deduce that it's something extremely primitive, however I still can't locate it.

    Thanks in advance for any help,

    Gabe
    Attached Files Attached Files

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can not [unless you are using rare and exclusive compiler] put your template function definition in a .cpp file [1], since the compiler must be able to see the template defintions when it compiles the code [unless you produce definitions for all of the specializations that you ever use, which goes against the idea of templates in the first place].

    [1] Unless you use #include "something.cpp" inside your .h file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Oh... I never realized that. Where should I put the #include in the file? I tried it and it gave me a ton of errors.... But thanks anyway

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Never include .cpp files. Put the template implementation into the header file, or perhaps a separate .ipp file that gets included. .cpp files are far too likely to be compiled explicitly.
    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

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    ...ipp?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ipp means "inline ++" or something like that, I think.

    I was a bit brief, I should have made a comment to say "don't do that".

    In this case, just copy and paste all of the .cpp content [except the #include of the template declarations themselves] into the header file, delete the .cpp file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Ok, thanks a lot )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Inheriting from specific templated version of class
    By skiingwiz in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2004, 08:21 AM
  4. Using a templated object in another class
    By harry_p in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2002, 11:35 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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