Like Tree7Likes
  • 1 Post By jimblumberg
  • 2 Post By King Mir
  • 1 Post By laserlight
  • 1 Post By phantomotap
  • 2 Post By King Mir

Template Linking Error

This is a discussion on Template Linking Error within the C++ Programming forums, part of the General Programming Boards category; First post here. I think I am missing a "T" somewhere. Anyway, this is just a trivial container task that ...

  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
    2,564
    When dealing with templates both the definition and the implementation must be in the same compilation unit (usually the same file).

    Jim
    King Mir likes this.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    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
    20,980
    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.
    Ralph Wiggum likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    3,829
    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
    Ralph Wiggum likes this.

  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,012
    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
    stahta01 and smokeyangel like this.
    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

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