我尝试了向前宣布,但没有成功。
我有两个类: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文件,所以我必须保持包含它,以及那些包含警卫。目前我通过将两个标头合并为一个来实现“工作”,但很明显,这看起来并不像是一个好的设计:)所以,我仍然会因为一个很好的解决方案而受到赞赏。
答案 0 :(得分:2)
如果你删除了包含,我相信它应该可行。
答案 1 :(得分:1)
不能从不同的名称空间转发声明类。
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;
}