Thread: Template Linking Error

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    7

    Template Linking Error

    First post here. I think I am missing a "T" somewhere. Anyway, this is just a trivial container task that holds one element. All I have written is a constructor. It works fine when the implementation is in the .h file, but when I try to place the constructor code into a separate .cpp, I get the following errors.

    C:\Users\Study\Documents\Study\study2\mycontainer. cpp|5|error: `template<class T> class mycontainer' used without template parameters|
    C:\Users\Study\Documents\Study\study2\mycontainer. cpp|6|error: ISO C++ forbids declaration of `mycontainer' with no type|
    C:\Users\Study\Documents\Study\study2\mycontainer. cpp|6|error: declaration of template `template<class T> int mycontainer(T)'|
    C:\Users\Study\Documents\Study\study2\mycontainer. h|8|error: conflicts with previous declaration `template<class T> class mycontainer'|
    C:\Users\Study\Documents\Study\study2\mycontainer. h|8|error: previous non-function declaration `template<class T> class mycontainer'|
    C:\Users\Study\Documents\Study\study2\mycontainer. cpp|6|error: conflicts with function declaration `template<class T> int mycontainer(T)'|
    C:\Users\Study\Documents\Study\study2\mycontainer. cpp||In function `int mycontainer(T)':|
    C:\Users\Study\Documents\Study\study2\mycontainer. cpp|7|error: `element' was not declared in this scope|
    C:\Users\Study\Documents\Study\study2\mycontainer. cpp|8|warning: no return statement in function returning non-void|
    ||=== Build finished: 7 errors, 1 warnings ===|

    Code is as follows...

    Code:
    // mycontainer.h
    #ifndef _MYCONTAINER_H
    #define _MYCONTAINER_H
    
    
    
    
    template <class T>
    class mycontainer
    {
        private:
            T element;
        public:
            mycontainer (T arg);
    };
    #endif
    Code:
    //  mycontainer.cpp
    #include "mycontainer.h"
    
    
    template <class T>
    mycontainer::mycontainer (T arg)
    {
        element = arg;
    }
    Code:
    // main.cpp
    #include "mycontainer.h"
    
    
    
    
    int main ()
    {
        mycontainer<int> myint (10);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When dealing with templates both the definition and the implementation must be in the same compilation unit (usually the same file).

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I suggest doing this:
    Code:
    // mycontainer.h
    #ifndef _MYCONTAINER_H
    #define _MYCONTAINER_H
    
    template <class T>
    class mycontainer
    {
        private:
            T element;
        public:
            mycontainer (T arg);
    };
    #include "mycontainer.hpp"
    #endif
    Code:
    //  mycontainer.hpp
    
    template <class T>
    mycontainer::mycontainer (T arg)
    {
        element = arg;
    }
    Code:
    // main.cpp unchanged
    #include "mycontainer.h"
    
    int main ()
    {
        mycontainer<int> myint (10);
        return 0;
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, change the _MYCONTAINER_H to say, MYCONTAINER_H. Names that begin with an underscore followed by an uppercase letter are reserved to the implementation for any use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    At least a part of the problem is that all the code posted so far is wrong.

    Code:
    template <class T>
    mycontainer::mycontainer (T arg)
    {
        element = arg;
    }
    -->

    Code:
    template <class T>
    mycontainer<T>::mycontainer (T arg)
    {
        element = arg;
    }
    Soma

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    These two changes made it work. Thanks.

    Quote Originally Posted by King Mir View Post
    I suggest doing this...
    Code:
    // ...
    #include "mycontainer.hpp"
    // ...

    Quote Originally Posted by phantomotap View Post
    O_o
    At least a part of the problem is that all the code posted so far is wrong.
    Code:
    template <class T>
    mycontainer<T>::mycontainer (T arg)

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Quote Originally Posted by laserlight View Post
    By the way, change the _MYCONTAINER_H to say, MYCONTAINER_H. Names that begin with an underscore followed by an uppercase letter are reserved to the implementation for any use.
    Thank you. I will do that from now on. I've seen reserved terms like __cplusplus which are proceeded by two underscores and a lower case letter. You're saying that I, the programmer, should never define something with an underscore followed by an upper case letter? Those symbols are reserved by the compiler/linker (is that you mean by "implementation"?)?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Right, as a programmer (and not a compiler writer), you shouldn't use names that start with a underscore followed by an upper-case letter, or contain a double underscore.

    This page has a more complete answer:
    standards - What are the rules about using an underscore in a C++ identifier? - Stack Overflow
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 12:31 PM
  2. Template specialization + linking error
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 06-06-2008, 12:03 PM
  3. Linking Error
    By saswatdash83 in forum C Programming
    Replies: 1
    Last Post: 04-19-2008, 03:18 PM
  4. Linking Error
    By Zoalord in forum Windows Programming
    Replies: 2
    Last Post: 06-09-2004, 05:23 PM
  5. Linking error?
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2002, 08:07 AM