Я пробовал простую декларацию, но это не сработало.
У меня было два класса: SDL_CTexture и SDL_CRenderer. Они не содержат ни одного экземпляра, указателя или ссылки друг на друга в качестве переменных-члена, но оба они имеют функцию-член, которая принимает параметр const и другого типа в качестве параметра, соответственно. См. Код ниже.
Это SDL_CTexture.hpp. Обратите внимание на прямое объявление в строке 20 и функцию в строке 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 };
Аналогично, SDL_CRenderer.hpp. См. Прямое объявление в строке 21 и функцию в строке 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;
}
Когда я попытался скомпилировать его, возникла ошибка:
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
Что я буду делать?
PS. Я должен скомпилировать каждый.cpp в свой собственный.o файл, чтобы я должен был включать в себя, как есть, а также эти защитники включения. В настоящее время я сделал это "работаю", объединив два заголовка в один, но ясно, что это не похоже на хороший дизайн :) Итак, я по-прежнему буду признателен за хорошее решение.
Класс не может быть объявлен вперед из другого пространства имен.
namespace A {
class X; // okay
};
class A::Y; // error
Это именно то, что вы здесь пытаетесь:
namespace sdl_cpp{ class SDL_CTexture; } class sdl_cpp::SDL_CRenderer;
и вы можете решить его, объединив их с
namespace sdl_cpp {
class SDL_CRenderer;
class SDL_CTexture;
}
Если вы удалите включенные, я считаю, что он должен работать.