0

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);
}
Ricardo_arg
  • 520
  • 11
  • 28

3 Answers3

4

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • sorry i didnt mentiod in the original post, but the guard are included – Ricardo_arg Jul 15 '13 at 15:43
  • @Ricardo_arg: Then please post some code (including the guards) that actually reproduces the error; we can only guess what's wrong with the code you don't show us. – Mike Seymour Jul 15 '13 at 15:56
3

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.

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

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

Ricardo_arg
  • 520
  • 11
  • 28