Ricardo_arg
Ricardo_arg

Reputation: 520

multiple definition in stl class header

I am getting stuck using this class, when I used in the main.cpp there is no problem and execute perfectly, but when I use it as a member class the compiler doesn't like it and sends the message "Multiple definition of:" Here is the class:

RTPSocket.h:

#ifndef RTP_SOCKET_HDR
#define RTP_SOCKET_HDR
    namespace RTPConnection
    {

    enum EMode
    {
        Sender,
        Receiver
    };

    template<EMode Mode>
    class RTPSocket
    {
    };
    }//end namespace


#define RTP_SOCKET_IMP
#include "RTPSocket_Imp.h"//file where i declare the implementation code
#undef RTP_SOCKET_IMP
#endif

this class by itself doesn't have any problem but when i use it in my class, but .... used in another class my file.h

#include RTPSocket.h 
class CommClass
{
private:
RTPSocket<RTPConnection::Receiver>  * mRTPServer;
}

the compiler give this error message: multiple definition of 'enum RTPConnection::EMode'

this is a method that is declared in another file "rtpsocket_imp.h" with the guard declared:

template<EMode Mode>
void RTPSocket<Mode>::write(char* aArray, 
                                      const size_t aiSize)
{
    std::string message("The write function is operative only on Sender Mode");
    throw BadTemplate(message);
}

Upvotes: 0

Views: 1020

Answers (3)

Ricardo_arg
Ricardo_arg

Reputation: 520

My problem was the CMakeLists.txt that was building this project. My fault!

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254451

You want include guards around the header:

#ifndef RTPSOCKET_H
#define RTPSOCKET_H

// header contents go here

#endif

This will prevent the header contents from being included more than once per source file, so you will not accidentally get multiple definitions.

UPDATE: Since you say you have include guards, then possible causes of the error might be:

  • misspelling the include guard name, so it doesn't work
  • defining something with the same name in another header (or the source file that includes it)
  • undefining the include guard name.

But without seeing code that reproduces the error, I can only guess what might be wrong.

Upvotes: 4

doctorlove
doctorlove

Reputation: 19232

You need an include guard. Inside the RTPSocket.h file at the top put

#ifndef RTPSOCKET_INCLUDED
#define RTPSOCKET_INCLUDED

and at the end put

#endif

If that's not the problem, and you do have an include guard, I suggest you search for

enum EMode

in your code and find all the places you have defined it, and make sure you just define it once.

Upvotes: 3

Related Questions