Thread: Template specialization + linking error

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Template specialization + linking error

    I keep getting a linking error for certain functions. Here's one:
    Code:
    Error	8	:StrTraits<char>::GetLength(char const *)" (?GetLength@?$StrTraits@D@Strings@@SAIPBD@Z) already defined in Help.obj	Help2.obj
    It's a template specialization, so it should reside in two headers.
    But as I include the header in two or more .cpp files, I get a linking error. Maybe someone can shed some light?
    Definition is as follows:

    Code:
    Code:
    	template<typename T> class StrTraits
    	{
    	public:
    		typedef int (fnc_tprintf_s)(T* buffer, size_t sizeOfBuffer, const T* format, ...);
    		typedef int (fnc_tvprintf_s)(T* buffer, size_t sizeOfBuffer, const T* format, va_list argptr);
    		typedef int (fnc_tvcprintf)(const T* format, va_list argptr);
    		typedef int (fnc_tcscmp)(const T* string1, const T* string2);
    	
    		static const T* UINT64_T_IDENTIFIER;
    		static const T* INT64_T_IDENTIFIER;
    		static const T* LONG_IDENTIFIER;
    	
    		static uint32_t GetLength(const T* strData);
    		static fnc_tprintf_s* GetFormatFunction(const T*);
    		static fnc_tvprintf_s* GetVFormatFunction(const T*);
    		static fnc_tvcprintf* GetVFormatCountFunction(const T*);
    		static fnc_tcscmp* GetCompareFunction(const T*);
    	
    		static T* Allocate(uint32_t nSize);
    		static void Delete(T* pToDelete);
    	};
    
    	template<> uint32_t StrTraits<char>::GetLength(const char* strData) { return strlen(strData); }
    	template<> uint32_t StrTraits<wchar_t>::GetLength(const wchar_t* strData) { return wcslen(strData); }
    (Yes, I know it's the wrong way to do traits, but I'm going to change it later.)
    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.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Make GetLength() impl. inline - since it's in a header.
    Or move it to a CPP.

    gg

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Even though it's a template? Or perhaps it simply doesn't apply to specializations?
    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.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It's the same as if you had put this in a header:
    Code:
    uint32_t GetLength(const wchar_t* strData) 
    { 
       return wcslen(strData);
    }
    Then 2 CPP's include it. Now you have 2 implementations.

    I don't think there's an exception for static members of a template or a specialization.

    gg

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I know that it would generate an error if it was a non-template function, but apparently specializations are considered to be real functions if instantiated, thus subject to the same rules as all normal functions.
    Because it doesn't apply to template functions (non-specialized, of course).
    Anyway, inlining them compiles it fine.
    So... you can actually put specializations in a source file. I guess it makes sense...
    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.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Full specializations indeed aren't templates. Why would they be? There's no uncertainty about them left.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM