Archived

This topic is now archived and is closed to further replies.

p_mxv

C++ trouble with OOP

Recommended Posts

They follwing code will not reconize, the CScheduler class. I have had this problem consistently and want to finally figure out what I am doing wrong. Any help would be appreciated, it errors when i compile CScheduler but there is nothing in CScheduler that would cause that error it has to be caused somewhere below. MAIN.H HERE #ifndef MAIN_H #define MAIN_H #include <GL/glut.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include "3DMath.h" #include "Graphics.h" #include "CScheduler.h" void displayUpdate(); void keyboardUpdate( unsigned char key, int x, int y ); CScheduler *scheduler; #endif CSCHEDULER.CPP

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Many questions are left open, I''m afraid:

- What does the compiler tell you when it does not recognize CScheduler?

This gives us a better understanding of your problem.

- What''s the content of your own header files?

They might influence the result of your compilation process. (No need to post them fully here, just go over them one by one.) You could try commenting them out one by one, just to see if your problem with Main.h and CScheduler goes away.

Basicly, more information is required, before an answer can be provided.

Ink

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Certainly

f:\programming\cg_toolkit\r1\main.h(17) : error C2143: syntax error : missing '';'' before ''*''
f:\programming\cg_toolkit\r1\main.h(17) : error C2501: ''CScheduler'' : missing storage-class or type specifiers
f:\programming\cg_toolkit\r1\main.h(17) : error C2501: ''scheduler'' : missing storage-class or type specifiers

are the 3 errors i get. Fairly confident that the problem exists in main.h but i cannot figure out why

Share this post


Link to post
Share on other sites
I sometimes get this and it really bugs me. Perhaps one of the files you are including is referencing CShedule before it is defined in CShedule.h. What I sometimes do in my headers is:



#ifndef _THISHEADER
#define _THISHEADER

// declare some classes before including anything

class myclass1;
class myclass2;

// now include stuff

#include something
#include soethingelse

// now define the classes

class myclass1
{

}

class myclass2
{

}

...


Don''t know if this helps at all!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try moving the CScheduler.h include further up in that list of includes.

Share this post


Link to post
Share on other sites
Check that your include guards do not have the same name in different header files.

You probably used _THISHEADER in more than one header file.
Better name the guard with the file or class name.

Share this post


Link to post
Share on other sites
I think you''re problem is that your classes are spider webed together somehow. Because of this you may have a situation where headerone.h requires headertwo.h above it, and headertwo.h requires headerone.h above it.

The way to get around this is to re-include the header file in the other header file. OR, if it just requires the class deffinition you can do a class prototype

eg.

class CClass; // At the start of the header code.

These class prototypes do not work in the corrisponding .cpp for that header file, because the cpp needs an actual deffinition, so #include "whatever.h" must be in the cpp.

Here''s an example... hopefully this helps.


// header 1


class CHeaderTwo; //Class prototype from header 2


class CHeaderOne{
//...

CHeaderTwo *HeaderTwo;
}

// header 2


class CHeaderOne; //Class prototype from header 1


class CHeaderTwo{
//...

CHeaderOne *HeaderOne;
}


// Main.cpp


#include "headerone.h"
#include "headertwo.h" // now these can be defined in any order.



Remember the headerone.cpp and headertwo.cpp still needs to include the whole headers.

- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
Certainly

f:\programming\cg_toolkit\r1\main.h(17) : error C2143: syntax error : missing '';'' before ''*''
f:\programming\cg_toolkit\r1\main.h(17) : error C2501: ''CScheduler'' : missing storage-class or type specifiers
f:\programming\cg_toolkit\r1\main.h(17) : error C2501: ''scheduler'' : missing storage-class or type specifiers

are the 3 errors i get. Fairly confident that the problem exists in main.h but i cannot figure out why


Right, so in main.h CScheduler is not known after the inclusion of CScheduler.h. You therefore included main.h in CScheduler.h which is what GeekPlusPlus pointed out.

If you must use this global scheduler pointer *shivers* then try prototyping it by placing the line

class CScheduler;

above it. This tells the compiler that there will be a class CScheduler, and as long as you refer to it as a pointer, the compiler doesn''t care what the implementation is.

In addition, declare it extern in main.h and actually implement it in your main.cpp (if any):

class CScheduler;
extern CScheduler *scheduler;

Even better would be using a Singleton pattern, but you should probably wait with that.

hth
Ink

Share this post


Link to post
Share on other sites