Initializing enums, specifically TTF_Font in SDL

Started by
3 comments, last by Retrothomas 12 years, 11 months ago
I'm having trouble setting up an object/enum for a TTF_Font in SDL. I'm following Lazyfoo's tutorial here:
http://lazyfoo.net/SDL_tutorials/lesson07/index.php

I'm trying to declare a TTF_Font in my header file so I can set the font in the constructor and use it in the draw method. I'm doing this:
TTF_Font* font;

Which doesn't work. I realize TTF_Font is an enum and from what I understand you can't declare enums like you can objects or other variables. So how can I create a variable to hold the font and set it up?

Thanks in advance!
Advertisement
"Which doesn't work" is not helpful information. Is it a compiler error? A run-time error? What messages are printed to the output of your compilation or execution before the failure?


I'm going to take a complete stab in the dark and say the problem is you're not including the SDL_ttf library header file, so its not finding the TTF_Font struct (its a struct, not an enum). If its not that, then you're not linking the TTF library correctly in your build.


This topic sounds like it belongs in the For Beginners section of the forums. This problem doesn't have anything to do with SDL or SDL_ttf really, at least from what I can gather.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Thanks for your reply, Roots. I posted after a long troubleshooting session and my brain had turned to mush! Let me clarify:

The specific error I received while compiling is "[font=Monaco]error: ISO C++ forbids declaration of 'TTF_Font' with no type[/font]"

I'm confident I have the SDL_TTF framework linked properly. I used the same procedure to link that and the SDL_Image framework and SDL_Image works fine. I have the header file included for SDL_TTF because I'm able to call TTF_Init(). If I remove the SDL_ttf header then I can't call TTF_init().

I just solved the problem, though. I had included the SDL_ttf header in another file and not the one for the class I was creating the SDL_ttf struct in.

Sometimes I miss something simple like that after programming for a while. I thought it was already included since it was in a global header but I missed it somehow. Thanks again :)
The problem is the compiler does not see the declaration of TTF_Font at the point you are using it. So you are including SDL_ttf.h in a manner that prevents the compiler from seeing this declaration when needed - which is in a particular header file. By the time you get inside a source file this is often worked out. See here for an example of how an apparently sane header setup confuses the compiler once the code has been preprocessed.

I recommend reading this article, as structuring your headers and source files correctly should solve this problem.

The problem is the compiler does not see the declaration of TTF_Font at the point you are using it. So you are including SDL_ttf.h in a manner that prevents the compiler from seeing this declaration when needed - which is in a particular header file. By the time you get inside a source file this is often worked out. See here for an example of how an apparently sane header setup confuses the compiler once the code has been preprocessed.

I recommend reading this article, as structuring your headers and source files correctly should solve this problem.


Thank you very much! I was doing most of the things in the article (resolving cyclic dependencies, using header files, and using #ifndef) but I tried to include too many things in a global header. I think I'll have to include files more locally in the other classes that need them and that should resolve this type of issue better.

This topic is closed to new replies.

Advertisement