0

I have a program with several classes and ive linked them all together in one main header:

#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"

I then have a link to main.h in each of those header files. Everything links together except when i try to declare

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

I get that each of them uses an undefined class "LTexture". I know im using forward declaration and include at the same time but this way gave me the least errors, just using include or forward declarations gave even more errors. The reason im declaring these in the header is because they're used in each of the other classes.

The texture class im just using from lazyfoo's tutorial

#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

Why is LTexture undefined if i have it linked to a working texture class

i tried placing all the extern definitions in one cpp and what i found is that it removes the errors from just that one cpp. So i split all the definitions to the specific class that they are needed however they're still needed in more than one and declaring them twice causes errors too. So is it a problem with how i linked headers? The way i set up my headers is each cpp references its header then the header references main.h. With main.h referencing every other header

MolerC
  • 67
  • 8

1 Answers1

0

You cannot define variables with an undefined (forward-declared) type because the compiler does not know how large they should be or how to call their constructor/destructor.

Moreover what you did won't work, each .cpp will have its own copy of the variables. What you want is extern LTexture gTileTexture; and putting the definition in one .cpp.

StenSoft
  • 9,369
  • 25
  • 30