Thread: template redefinition

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    template redefinition

    Hi!

    I will try to explain my problem in the simplest way. I have a template class Queue, as follows:
    Code:
    template<class T>
    class Queue{
    
     public:
    
      explicit Queue();
    
      void add(T);
      T get();
    
     private:
    
      sem_t sem_struct;
      sem_t sem_count;
      list<T> elements;
      
      Queue(Queue&);
      Queue& operator=(const Queue&);
    };
    // definitions...
    The definitions are inside this file. Other two files include this header. There is other file that includes these two files. I compile each file separately. The two files that include the template compile succesfully. But, when I try to compile the third file, I have compiler error:

    In file included from ether.h:15,
    from host.cpp:2:
    fila.h:8: redefinition of `class Queue<T>'
    fila.h:8: previous definition of `class Queue<T>'

    The general structure of the files are:

    ether.h includes queue.h
    application.h includes queue.h
    host.cpp includes application.h and ether.h


    Thanks any help!

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    header files need to be protected to make sure that they aren't getting linked more than once, which in this case it's being linked twice. At the top of your header file, place the following two lines:

    Code:
    #ifndef _THIS_STRING_DOESNT_MATTER_AS_LONG_AS_ITS_UNIQUE
    #define _THIS_STRING_DOESNT_MATTER_AS_LONG_AS_ITS_UNIQUE
    and place
    Code:
    #endif
    at the bottom of the header. When your compiler is going through and linking everything, it will come to this file, see that _THIS_STRING_DOESNT_MATTER_AS_LONG_AS_ITS_UNIQUE is not yet defined, then define and then define your class using your declaration. Every time after that, it tries to link to the header, sees that _THIS_STRING_DOESNT_MATTER_AS_LONG_AS_ITS_UNIQUE is now defined, and does try to define it again.

  3. #3
    Registered User Dogtree's Avatar
    Join Date
    May 2005
    Posts
    1
    >>_THIS_STRING_DOESNT_MATTER_AS_LONG_AS_ITS_UNIQ UE
    It does matter because a leading underscore in this context is reserved by the implementation.

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I was not sure if this would work! Thanks!

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    an advice
    Do void add(const T&);
    Your implementation creates a new T var that is passed to the method, which causes a overhead. Using const T& only a pointer is passed to the class. No copy of the passed var is created, therefore it's more efficient. In the end 'elements[some_index]=passed_var'. So either way you'll have a copy of the original object in the list.

  6. #6
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by xErath
    an advice
    Do void add(const T&);
    Your implementation creates a new T var that is passed to the method, which causes a overhead. Using const T& only a pointer is passed to the class. No copy of the passed var is created, therefore it's more efficient. In the end 'elements[some_index]=passed_var'. So either way you'll have a copy of the original object in the list.
    Yes! Very good advice! Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM