×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    30

    How to get resolve with function redefinition error, while using function templates.

    I am getting sick with this error, function Redefinition !!! .

    I am building a log file, So that i can keep track of my program memroy allocation and deallocation,
    to get rid of memory leaks.For that i have build a function template in a Namespace. What i am doing is that , i will pass

    Object to this function, the object could be of any type, and getting information about that Object.


    // MemTrack.h


    #ifndef MEMTRACK_H
    #define MEMTRACK_H

    #include <f32file.h>

    namespace MemTracker
    {

    void LogInit(const char* logName = NULL)
    {
    //Doing Something with logName
    }

    template<class T>

    void MemLog( T* mVar )
    {
    // Doing something with mVar
    }

    };

    #endif

    Now when i include this MemTrack.h in one file it is fine

    // Myclass.cpp

    #include "MemTrack.h"

    MyClass::MyClass()
    {
    MemTracker::MemLog(this); //Working fine , no error
    }


    But when i include this MemTrack.h file in another file(s) it give me error given below

    error LNK2005: "void __cdecl MemTracker::LogInit(char const *)" (?LogInit@MemTracker@@YAXPBD@Z) already defined in

    SEARCHFILECONTAINER.obj

    PLease help me , and tell me how could i able to get access this function in multiple files.


    ..... Thanks in advance

    with best regards

    chetan

  2. #2
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122
    void LogInit(const char* logName = NULL)
    {
    //Doing Something with logName
    }
    In here you define the function body (and not just declare the function signature). Either move the function definition (the body, leave the declaration) to a .cpp file or inline the function.

    Lauri

  3. #3
    Registered User
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    30
    Many Thanks for your reply,

    But i have already seen it by implementing like this , it is also not working ,

    Well the actual problem is with template function , when i compile it, by passing it with one type, it internally build the copy of the function with that type , and now when in another file when i try to redefine it with the same type , it give me redefinition error.

    Well it is fine working after declaring this template function as static.But this is not the proper way because it take increases the code size too much.

    Can there be another way to resolve this problem?

    Once again thanking u

    chetan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×