LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Compilation Error (https://www.linuxquestions.org/questions/programming-9/c-compilation-error-575338/)

kamransoomro84 08-15-2007 02:34 PM

Well, I've managed to strip it to just a few files. I'm posting the code below.

kamransoomro84 08-15-2007 02:40 PM

File: reliableudp.h
Code:

#ifndef RELIABLEUDP_H_
#define RELIABLEUDP_H_


// Forward declarations to pacify the compiler :)
namespace ReliableUDP
{
        class BaseSocket;
        class Socket;
        class ServerSocket;
}


#include "BaseSocket.h"
#include "Socket.h"
#include "ServerSocket.h"


#endif /*RELIABLEUDP_H_*/

File: BaseSocket.h
Code:

#ifndef BASESOCKET_H_
#define BASESOCKET_H_

#include "reliableudp.h"
#include <string>

using std::string;

namespace ReliableUDP
{
        class BaseSocket
        {
        };
}
               
#endif /*BASESOCKET_H_*/

File: ServerSocket.h
Code:

#ifndef SERVERSOCKET_H_
#define SERVERSOCKET_H_

#include "reliableudp.h"
#include <string>


namespace ReliableUDP
{

        class ServerSocket : public BaseSocket
        {
        };

}

#endif /*SERVERSOCKET_H_*/

File: Socket.h
Code:

#ifndef SOCKET_H_
#define SOCKET_H_

#include "reliableudp.h"
#include <string>

namespace ReliableUDP
{

        class Socket : public BaseSocket
        {
        };

}

#endif /*PEER_H_*/


kamransoomro84 08-15-2007 02:41 PM

The above is a complete program. I'm compiling it as a shared library. As you can see, it's just a skeleton program. But I still get the error.

kamransoomro84 08-15-2007 02:43 PM

I forgot to mention, I'm using the cmake utility. The CMakeLists.txt file is written as follows:
Code:

SET(RUDP_SRC
BaseSocket.cpp
ServerSocket.cpp
Socket.cpp
)

ADD_LIBRARY(rudp SHARED ${RUDP_SRC})

The corresponding .cpp files are empty. They do not contain anything.

graemef 08-15-2007 06:08 PM

guards are the combination #ifndef with #define and #endif which wraps around the definition in the header files. Hence BaseClass.h would be:
Code:

#ifndef BASE_CLASS
#define BASE_CLASS

#include <iostream>

using namespace std;

class BaseClass
{
  protected:
      int number;
  public:
      BaseClass(int num = 1){number = num;}
      int num () {return number;}
};

#endif

The identifier (for example BASE_CLASS) needs to be unique for every header file that you have.

kamransoomro84 08-16-2007 05:49 AM

Well, I tried changing the guard, just to be sure it wasn't conflicting with any other. The problem persists.

suliman_shah 08-17-2007 03:01 AM

whatever i think is that you have saved your code in .c file if its so then change it to cpp. its just a guess from my side.

kamransoomro84 08-17-2007 06:21 AM

Nopes. All the code is in .cpp files.

kamransoomro84 08-17-2007 01:48 PM

No one can help me?

graemef 08-17-2007 10:51 PM

On the little that you have given, you have had quite a bit of help. It's probably up to you now to help yourself and post some code...

kamransoomro84 08-18-2007 05:22 AM

But I already have posted some code. Scroll up and look for yourself. It fulfills all of your requirements. It's complete and concise. What more is required?

kamransoomro84 08-18-2007 05:24 AM

An update. On a fluke, I commented the line #include "reliableudp.h" in BaseSocket.h. The error disappeared. Unfortunately, I can't do this in the main project because it introduces a lot of dependency issues. Also, the same thing is done in all of the other files, but it doesn't give an error there. I do not understand what the problem is?

graemef 08-18-2007 11:22 PM

Quote:

Originally Posted by kamransoomro84 (Post 2862882)
But I already have posted some code. Scroll up and look for yourself.

I'm sorry I completely missed it... that'll teach me to jump to the next unread!

I'm not certain why you are using the forward declarations. I'm sure that it your problem. if you do need them then put them in the file that requires it, that is if ServerSocket also needs to know about Socket then put it in ServerSocket.h

graemef 08-19-2007 12:22 AM

Hi,

I've had a proper look at it now and managed to duplicate your problem. In C++ you can't have a forward declaration of a base class, that is the following will fail.

Code:

#ifndef SOCKET_H_
#define SOCKET_H_

class BaseSocket;

namespace ReliableUDP
{

        class Socket : public BaseSocket
        {
        };

}

#endif /*SOCKET_H_ */

Instead what you must have is:

Code:

#ifndef SOCKET_H_
#define SOCKET_H_

#include "BaseSocket.h"

namespace ReliableUDP
{

        class Socket : public BaseSocket
        {
        };

}

#endif /*SOCKET_H_ */

Where the file BaseSocket.h defines the class BaseSocket.

To recreate the problem I had the line #include "BaseSocket.h" in the file BaseSocket.cpp

Now what is happening with your code is as follows:
  • BaseSocket.cpp includes BaseSocket.h
  • BaseSocket.h includes reliableudp.h
  • reliableudp.h has forward declarations and then includes BaseSocket.h
  • The guards of BaseSocket.h stop the class BaseSocket from defined at this moment, and control is returned to reliableudp.h
  • reliableudp.h now includes Socket.h
  • Socket.h includes reliableudp.h
  • The guards of reliableudp.h stop further processing and control is returned to Socket.h
  • Socket.h attempts to define class Socket but it doesn't have a definition of BaseSocket hence the error.
What to do?
  1. Replacing the line #include "BaseSocket.h" with #include "reliableudp.h" in the file BaseSocket.cpp will possibly fix it
  2. Removing the file reliableudp.h and including the files that are needed when they are needed will fix it.
Personally I'd go with the second but you may find that the first approach works for you.

1slipperyfish 08-19-2007 05:22 AM

hello i'm no expert by i think they are refering to your guard
Code:

#ifndef BASE_CLASS_HPP
#define BASE_CLASS_HPP
#endif

i found out the hard way about guards, if you're using more than one header file you will need to include them, if you already have then i'm sorry for wasting your time :)
hth
paul


All times are GMT -5. The time now is 07:30 AM.