2015-11-25 28 views
0

我有几个类和IVE一个程序在一个主头部连接它们放在一起:标识符包括报头之后使用未定义类

#ifndef __MAIN_H_INCLUDED__ 
#define __MAIN_H_INCLUDED__ 

//Using SDL, SDL_image, standard IO, strings, and file streams 

#include <SDL.h> 
#include <SDL_image.h> 
#include <stdio.h> 
#include <string> 
#include <fstream> 
#include "ltexture.h" 
#include "tile.h" 
#include "player.h" 
#include "enemy.h" 
#include "egg.h" 

我然后有一个链接在每个这些头标对main.h文件。除了当我试图声明

class LTexture; 
LTexture gTileTexture; 
LTexture gPlayerSpriteSheetTexture; 
LTexture gEnemySpriteSheetTexture; 
LTexture gEggSpriteSheetTexture; 

我让他们每个人都使用一个未定义类“LTexture”一切都连接在一起。我知道即时通讯使用前向声明和包括在同一时间,但这种方式给了我最少的错误,只是使用包含或转发声明给了更多的错误。在头文件中声明这些的原因是因为它们被用在其他类中。

结构类IM刚刚从lazyfoo的教程

#ifndef __LTEXTURE_H_INCLUDED__ 
#define __LTEXTURE_H_INCLUDED__ 

#include "main.h" 

class LTexture 
{ 
public: 
    //Initializes variables 
    LTexture(); 

    //Deallocates memory 
    ~LTexture(); 

    //Loads image at specified path 
    bool loadFromFile(std::string path); 

    //Creates image from font string 
    bool loadFromRenderedText(std::string textureText, SDL_Color textColor); 

    //Deallocates texture 
    void free(); 

    //Set color modulation 
    void setColor(Uint8 red, Uint8 green, Uint8 blue); 

    //Set blending 
    void setBlendMode(SDL_BlendMode blending); 

    //Set alpha modulation 
    void setAlpha(Uint8 alpha); 

    //Renders texture at given point 
    void render(int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE); 

//Gets image dimensions 
int getWidth(); 
int getHeight(); 

private: 
    //The actual hardware texture 
    SDL_Texture* mTexture; 

    //Image dimensions 
    int mWidth; 
    int mHeight; 
}; 

#endif 

使用为什么LTexture不确定的,如果我有它连接到一个工作结构类

我试图把所有的extern定义在一个CPP什么我发现它是从那一个cpp中删除错误。所以我把所有的定义分割成了他们需要的特定类,然而他们仍然需要多于一个,并且声明它们两次也会导致错误。那么,我如何链接标题是一个问题?我设置我的头文件的方式是每个cpp引用其头文件,然后头文件引用main.h.使用main.h引用其他所有头文件

+0

OT:http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier –

+0

所以是错误,因为额外的__在前面定义语句 – MolerC

+0

不太可能,这只是一句话。你也不能在btw结尾有'__'。 –

回答

0

由于编译器不知道应该有多大或如何调用它们的构造函数/析构函数,因此无法定义具有未定义(前向声明)类型的变量。

此外,您所做的不会工作,每个.cpp都会有自己的变量副本。你想要的是extern LTexture gTileTexture;并把定义放在一个.cpp中。