Diaz
Diaz

Reputation: 987

How to solve mutual dependency in C++?

I tried forward declaration but it did not work.

I had two class: SDL_CTexture and SDL_CRenderer. They don't hold any instance, pointer, or reference to each other as member variables, but they both have a member function that takes a const& of the other type as a parameter, respectively. See the code below.

This is SDL_CTexture.hpp. Note the forward declaration at line 20 and the function in line 31.

  # ifndef SDL_CTEXTURE_HPP
10 # define SDL_CTEXTURE_HPP
11 
12 # include <SDL2/SDL.h>
13 # include "SDL_CRenderer.hpp"
14 # include <string>
15 
16 namespace sdl_cpp{
17   class SDL_CTexture;
18 }
19 
20 class sdl_cpp::SDL_CRenderer;
21 
22 class sdl_cpp::SDL_CTexture{
23 public:
24   SDL_CTexture();
25   ~SDL_CTexture();
26 
27   /* Create a texture from image file.                                     
28    * It calls SDL_LoadBMP internally, and takes care of freeing            
29    * temporary SDL_Surface.                                                
30    */
31   bool load(const char* filename, const sdl_cpp::SDL_CRenderer &renderer);
32 
33   SDL_Texture* get_texture() const;
34   int get_width() const;
35   int get_height() const;
36   static std::string CLASS;
37 
38 protected:
39   SDL_Texture* m_texture;
40   int m_w;/* texture width */
41   int m_h;/* texture height */
42 };

Similarly, SDL_CRenderer.hpp. See forward declaration at line 21 and function in line 35.

# ifndef SDL_CRENDERER_HPP
10 # define SDL_CRENDERER_HPP
11 
12 # include <SDL2/SDL.h>
13 # include "SDL_CWindow.hpp"
14 # include "SDL_CTexture.hpp"
15 # include <string>
16 
17 namespace sdl_cpp{
18   class SDL_CRenderer;
19 }
20 
21 class sdl_cpp::SDL_CTexture;
22 
23 class sdl_cpp::SDL_CRenderer{
24 public:
25   SDL_CRenderer();
26   ~SDL_CRenderer();
27 
28   /* wrappers */
29   bool create(const sdl_cpp::SDL_CWindow &window, int index = -1, Uint32 f\
   lags = 0);
30   void set_color(Uint8 r=0, Uint8 g=0, Uint8 b=0, Uint8 a=255);
31   void clear();
32   void present();
33 
34   /* copy the whole texture as it is, to destined coordinator */
35   bool copy(const sdl_cpp::SDL_CTexture &texture, int x, int y);
36 
37   SDL_Renderer* get_renderer() const;
38 
39   static std::string CLASS;
40 
41 protected:
42   SDL_Renderer* m_renderer;
}

When I attempted to compile it generated error:

g++ SDL_CTexture.cpp -Wall -c -std=c++11 `sdl2-config --cflags` -o SDL_CTexture.o
In file included from SDL_CTexture.hpp:13:0,
                 from SDL_CTexture.cpp:10:
SDL_CRenderer.hpp:21:16: error: ‘SDL_CTexture’ in namespace ‘sdl_cpp’ does not name a type
 class sdl_cpp::SDL_CTexture;
                ^
SDL_CRenderer.hpp:35:19: error: ‘SDL_CTexture’ in namespace ‘sdl_cpp’ does not name a type
   bool copy(const sdl_cpp::SDL_CTexture &texture, int x, int y);
                   ^
SDL_CRenderer.hpp:35:42: error: ISO C++ forbids declaration of ‘texture’ with no type [-fpermissive]
   bool copy(const sdl_cpp::SDL_CTexture &texture, int x, int y);
                                          ^
In file included from SDL_CTexture.cpp:10:0:
SDL_CTexture.hpp:20:16: warning: declaration ‘class sdl_cpp::SDL_CRenderer’ does not declare anything [enabled by default]
 class sdl_cpp::SDL_CRenderer;
                ^
make: *** [SDL_CTexture.o] Error 1

What am I gonna do?

PS. I have to compile each .cpp into its own .o file so I must keep inclusion as it was, as well as those inclusion guards. Currently I made it 'work' by merging two headers into one, but clearly, this does not look like good design:) So, I will still be appreciated for a nice solution.

Upvotes: 0

Views: 196

Answers (2)

user743382
user743382

Reputation:

A class cannot be forward-declared from a different namespace.

namespace A {
  class X; // okay
};

class A::Y; // error

This is exactly what you're trying here:

namespace sdl_cpp{
  class SDL_CTexture;
}

class sdl_cpp::SDL_CRenderer;

and you can solve it by combining them to

namespace sdl_cpp {
  class SDL_CRenderer;
  class SDL_CTexture;
}

Upvotes: 1

Cristian Bidea
Cristian Bidea

Reputation: 709

  1. If you forward declare SDL_CREnderer (Line 20) you don't have to include the header anymore (Line 13). That's why you are using forward declarations.
  2. The same thing is true for SDL_Ctexture Line 14 and 21

If you remove the includes, I believe it should work.

Upvotes: 2

Related Questions