Sign in to follow this  
EvilNando

c++ compilation bug

Recommended Posts

Okay this thing is really starting to get into my nerves....

I have 2 classes

Application and ModuleVideo


my Application class contains an instance of the ModuleVideo, the thing is that sometimes at compile time my compiler tells me that ModuleVideo needs a reference which is strange as I have the proper #include "ModuleVideo.h" at the header of Aplication class

but heres the funny thing:

if I comment the class and recompile (gets me some more errors) and then I uncomment and try to recompile again EVERYTHING WORKS OK

why is this?

Share this post


Link to post
Share on other sites
vs2010

pretty simple stuff


[code]


#ifndef APPLICATION_H_
#define APPLICATION_H_

#include "Common.h"
#include "ModuleVideo.h"

class Application
{
public:
// stuff
ModuleVideo module_video;
// more stuff
}

#endif



#ifndef MODULE_VIDEO_H_
#define MODULE_VIDEO_H_

#include "Common.h"

// stuff

#endif

[/code]


the error:

[code]

Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...
Error 1 error C2146: syntax error : missing ';' before identifier 'module_video' ....

[/code]

Share this post


Link to post
Share on other sites
[quote name='EvilNando' timestamp='1305650847' post='4811984']

if I comment the class and recompile (gets me some more errors) and then I uncomment and try to recompile again EVERYTHING WORKS OK

[/quote]


Try cleaning and rebuilding if that happens. See if if that helps.

Share this post


Link to post
Share on other sites
Now you are not reproducing the full source. We are not psychic - we cannot divine problems in code we cannot see. Does Common.h include either Application.h or ModuleVideo.h, directly or indirectly? What does Common.h have in it?

Share this post


Link to post
Share on other sites
Well what's in "//stuff" ? and in Common.h ?

A good thing in general is to reduce/simplify your *real* code, while still keeping the problem, and then show us the *whole* thing.

Share this post


Link to post
Share on other sites
I will be very impressed if the problem lies on the code Ive omitted honestly

theres no way it should affect the compilation behavior, but what do I know...
[code]

#include <SDL.h>

#include <stdio.h>
#include <stdarg.h>
#include <fstream>
#include <sstream>
#include <string>

#include "Application.h"

// APPLICATION ERROR CODES
#define ERR_CODE_NONE 0

#define ERR_CODE_SDL_EVENT 1
#define ERR_CODE_SDL_INIT 2

#define ERR_CODE_MODULE_VIDEO 3
#define ERR_CODE_MODULE_VIDEO_SHADERS 4

#define ERR_CODE_FILE_NOT_FOUND 5
#define ERR_CODE_EMPTY_FILE 6

#endif
[/code]

[code]

#ifndef APPLICATION_H_
#define APPLICATION_H_

#include "Common.h"
#include "ModuleVideo.h"

class Application
{
public:
Application();
~Application();

void Init(void);
int Run(void);
void ProcessEvent(SDL_Event*);

static void ShowMessage(char* msg, ...);

private:

bool is_running;
SDL_Event sdl_event;
int error_code;

ModuleVideo module_video;

};

#endif // APPLICATION_H_
[/code]


[code]

#ifndef MODULE_VIDEO_H_
#define MODULE_VIDEO_H_

#include "Common.h"
#include <GL/glew.h>


#define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 600

class ModuleVideo
{

public:
ModuleVideo();
~ModuleVideo();

Uint32 Init(void);
Uint32 Init(Uint32, Uint32);

void StartRender(void);
void EndRender(void);

Uint32 CreateShaderProgram(GLint&, const char*, const char*);

private:

Uint32 LoadShaderFile(const char*, std::string&);

union
{
Uint32 width;
Uint32 height;

} resolution;

SDL_Surface* sdl_surface;

};

#endif
[/code]

Share this post


Link to post
Share on other sites
Is the first file Common.h ?
If so, it's include guard is broken. After fixing it, I can make the thing compile.
(by the way, why is it including Application.h ?)

Looks like you removed too much code again. It's nice to simplify, so that we don't have to read pages and pages of code, but you have to keep the problem present (just test the simplified version before posting it).
Sometimes this process of simplifying will allow you to find the problem yourself.

Share this post


Link to post
Share on other sites
First, you must realise that the preprocessor is simply glorified copy and paste. When it sees #include, it preprocesses the given file and pastes the source into the including file.

Consider what happens when ModuleVideo.cpp includes ModuleVideo.h
[source]
module video.h:
> #define MODULE_VIDEO_H_
> #include Common.h
Common.h:
> #define COMMON_H_
> #include standard headers
> #include Application.h
Application.h:
> #define APPLICATION_H_
> #include Common.h
common.h:
> COMMON_H_ already defined, do nothing
> class Application {
> // ...
> ModuleVideo module_video;

// And so on

[/source]
We can see that the preprocessed file (the "translation unit" the compiler actually sees) appears to start with the standard header file definitions, and then the Application class. The Application class references the (as yet undeclared) ModuleVideo class.

This is mostly because C++ continues to use a compilation model designed for 1970s computers.

Other points:
[list]
[*]You don't want to use a "union" for the resolution member. Find out what unions mean, then use a struct or a class.
[*]Consider using an enumeration for your error codes
[*]Likewise use an enum or const integers for your DEFAULT WIDTH/HEIGHT
[*]You should include C++ headers such as <cstdio> rather than <stdio.h>
[*]Aggregating header files like "Common.h" should either:
[list]
[*]Include your own header files but never be included from them
[*]Exclude your own header files but may be included from them (e.g. it might reference the standard headers and any libraries you are using)
[/list]
[*]In the latter case you could consider using precompiled headers to speed up your compilation times.
[/list]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this