Jump to content
  • Advertisement
Sign in to follow this  
slanker70

An Odd error

This topic is 3494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 destructor
	Text(GLint in_fontSize, GLint in_fontSpace, string in_filename, string in_fontType);
	~Text();

public:

	// Member functions

	// Build font texture using display list
	GLvoid buildTextureFont();

	// Draw text
	GLvoid drawText(GLint x, GLint y, const char* text, ...); // Draw in 2D, must be preceded by iEngineCore->draw2D()

	// Get text width

	GLuint getTextWidth(const char* text);

	// Get text height
	GLuint getTextHeight(const char* text);

	// Set font texture
	GLvoid setFontTexture(string fileName);

	// Get font type
	string getFontType();

	// Get font space
	GLsizei getFontSpace();

	// Vector of class instances
	static vector<Text*> texts;

private:

	// Member variables

	// The ID that points to a particular font base (display list)
	GLuint fontBase;

	// Stores Font texture
	Texture* fontTexture; <-------------- THE PROBLEM IS HERE

	// Stores font size
	GLsizei fontSize;

	// Stores font type
	string fontType;

	// Stores font space
	GLsizei fontSpace;
};

#endif
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 '*' Whereas in Xcode (OSX) and CodeBlocks (Linux) the error is: iso c++ forbids declaration of Texture with no type The 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]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by slanker70
Hi,

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 slanker70
The 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)...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Does Texture.h also include EngineCore.h?

This is why so called "master headers" aren't usually a good idea.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 line

Texture* fontTexture; <-------------- THE PROBLEM IS HERE

to

class Texture* fontTexture; <-------------- THE PROBLEM IS HERE

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!

Share this post


Link to post
Share on other sites
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 Lascha
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.


I use this pattern heavily for organizing class definitions that have a lot of member functions.

Share this post


Link to post
Share on other sites
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 line

Texture* fontTexture; <-------------- THE PROBLEM IS HERE

to

class Texture* fontTexture; <-------------- THE PROBLEM IS HERE


What'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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!