   An Odd error
  Hi,I've encountered an odd error regarding a Class#ifndef TEXT_H#define TEXT_H// Header files#include "EngineCore.h"class Text{public:// Constructor and destructorText(GLint in_fontSize, GLint in_fontSpace, string in_filename, string in_fontType);~Text();public:// Member functions// Build font texture using display listGLvoid buildTextureFont();// Draw textGLvoid drawText(GLint x, GLint y, const char* text, ...); // Draw in 2D, must be preceded by iEngineCore-&gt;draw2D()// Get text widthGLuint getTextWidth(const char* text);// Get text heightGLuint getTextHeight(const char* text);// Set font textureGLvoid setFontTexture(string fileName);// Get font typestring getFontType();// Get font spaceGLsizei getFontSpace();// Vector of class instancesstatic vector&lt;Text*&gt; texts;private:// Member variables// The ID that points to a particular font base (display list)GLuint fontBase;// Stores Font textureTexture* fontTexture; &lt;-------------- THE PROBLEM IS HERE// Stores font sizeGLsizei fontSize;// Stores font typestring fontType;// Stores font spaceGLsizei fontSpace;};#endifTexture.h is included within EngineCore.h so it should not be a problem.The error in Visual Studio 2008 is:error C2143: syntax error : missing ';' before '*'Whereas in Xcode (OSX) and CodeBlocks (Linux) the error is:iso c++ forbids declaration of Texture with no typeThe way I solved the problem in Visual Studio and Xcode is by commenting out the line, build, un-comment it, and build again. But that method doesn't solve the problem in CodeBlocks.Can anyone please help me.Thank you,Dana[edit] Added source tags - Apoch[Edited by - ApochPiQ on May 20, 2009 10:59:27 PM]
Quote:Original post by slanker70Hi,I've encountered an odd error regarding a Class[snip]Texture.h is included within EngineCore.h so it should not be a problem.The error in Visual Studio 2008 is:error C2143: syntax error : missing ';' before '*'Sounds like you have a cyclic include going on - EngineCore.h includes Texture.h and Texture.h includes EngineCore.h, or something similar, and the include guards prevent the compiler from bailing / getting into an infinite loop.Quote:Original post by slanker70The way I solved the problem in Visual Studio and Xcode is by commenting out the line, build, un-comment it, and build again. But that method doesn't solve the problem in CodeBlocks.That doesn't sound like it's solved the problem, that sounds like you've somehow broken your compiler(s)...   ;  Hi and thanks for the reply,Just tried your suggestion and instead of including "EngineCore.h" I've included "Texture.h" directly into the class, same problem though for some reason?Regards,Dana   ;  Does Texture.h also include EngineCore.h?This is why so called "master headers" aren't usually a good idea.   ;  Hey, you wrote "public:" twice in your class. I don't know if that's an error but I've never seen that before.Edit: Ah, I just compiled some code out of curiousity. It's not an error, my apologies. Was not aware of this.   ;  Forward declarations could save you here since it's a pointer to the type.IE before that class where the problem is put this:class Texture;another thing that might work is change this lineTexture* fontTexture; <-------------- THE PROBLEM IS HEREtoclass Texture* fontTexture; <-------------- THE PROBLEM IS HEREoh and personal pet peeve, why do you put the * next to the type?that implies...int* a,b; //will this give you 2 pointers? no!when...int *a,*b; //the * is a modifier of the variable being declared, not the type!;  Added source tags to the OP - please check the FAQ on how to add your own tags in the future [smile]Quote:Original post by LaschaHey, you wrote "public:" twice in your class. I don't know if that's an error but I've never seen that before.Edit: Ah, I just compiled some code out of curiousity. It's not an error, my apologies. Was not aware of this.I use this pattern heavily for organizing class definitions that have a lot of member functions.   ;  Hi,Thank you all for your replies and sorry for not using any code tags, won't happen again [smile]Quote:This is why so called "master headers" aren't usually a good idea.I've altered all my codes to include individual header files directly rather then using "master headers" and it solved the problem for every pltaform.But why does the error occured only for that particular class? Cause I do have other classes in my game engine that has a member of other Class instance pointer.Quote:Forward declarations could save you here since it's a pointer to the type.IE before that class where the problem is put this:class Texture;So if I do use forward declaration, do I still need to include the necessary header files?Quote:another thing that might work is change this lineTexture* fontTexture; <-------------- THE PROBLEM IS HEREtoclass Texture* fontTexture; <-------------- THE PROBLEM IS HEREWhat's the difference between the above method and the first one, is it just a matter of preference?Quote:oh and personal pet peeve, why do you put the * next to the type?that implies...int* a,b; //will this give you 2 pointers? no!when...int *a,*b; //the * is a modifier of the variable being declared, not the type!I got use to type that way following a tutorial when I first learn C++. I'll try to remember that from now on [smile]Also in terms of optimisation is it better to use "master headers" or forward declarations?And why does my quotes doesn't display the original poster, do I have to type it manually?Regards,Dana;Quote:But why does the error occured only for that particular class? Cause I do have other classes in my game engine that has a member of other Class instance pointer.Sometimes, you get lucky and the include order doesn't break anything.Quote:So if I do use forward declaration, do I still need to include the necessary header files?It depends. Sometimes forward declarations won't work, for example you can't forward declare a member you hold by value. Implementing functions inside the class declaration can also cause issues.But a lot of the time, once you forward declare a class Foo, you will probably get away with moving the #include "foo.h" to the source file.Quote:What's the difference between the above method and the first one, is it just a matter of preference?The second doubles as a forward declaration. C++ lets you do odd things like that.Quote:Also in terms of optimisation is it better to use "master headers" or forward declarations?Forward declarations are best, they reduce the number of times a file is #included and can speed up compilation.As for master headers, they are good for "external code". What I mean is, write you code normally, with no master headers. Use forward declarations where possible. Then, when code external to that "module" or "library" (for example, non graphics code accessing the graphics types) wants to use it, you can create a master header for the other code.This way, once your "module" is completed you can include a single header that gives you access to all the types. But none of the files inside the module should include the master header.The nice thing about this way is that it is optional: if I want to use your engine I can include everything by saying #include "slanker/engine.hpp". But if I only want to include a small number of types I can do that too, e.g. #include "slanker/text.hpp".Quote:And why does my quotes doesn't display the original poster, do I have to type it manually?If you type the quote tags manually, you have to enter the posters name. If you click the "quote" link when replying, the forum software will auto-populate your post with something like this:Quote:Original post by gamedev.net[quote][i]Original post by rip-off[/i]rip-off writes a long and tedious reply...[/quote]You can fill it in yourself if you want, but its a lot of effort.   ;  Hi,Thanks for the reply rip-off.Here's the schematic of what I did.Quote:Text.h - uses forward declaration "class Texture"Text.cpp - includes "Text.h" and "Texture.h"Control.h is an abstract class - uses forward declaration "class Text"Control.cpp - includes "Control.h"Button.h is a child of Control - includes "Control.h"Button.cpp - includes "Button.h" and "Text.h"It compiles fine but please let me know if there's a better way to do it.Regards,Dana   