Public Group

#### Archived

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

# C++ trouble with OOP

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

## 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 on other sites
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.

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.

Ink

##### Share on other sites
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 on other sites
In the file "CScheduler.h", check to see if you are missing a semicolon at end of class declaration.

##### 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 anythingclass myclass1;class myclass2;// now include stuff#include something#include soethingelse// now define the classesclass myclass1{}class myclass2{}...

Don''t know if this helps at all!

##### Share on other sites
try moving the CScheduler.h include further up in that list of includes.

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

Better name the guard with the file or class name.

##### Share on other sites
Is CScheduler in a namespace?

FYI, you want to use the standard C++ header files, cmath, cstdlib, and cstdio (not math.h, stdlib.h, and stdio.h). Everything defined in them is in namespace std.

--
Dave Mikesell Software & Consulting

[edited by - dmikesell on January 6, 2004 7:39:56 AM]

##### 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 1class CHeaderTwo;  //Class prototype from header 2class CHeaderOne{//...CHeaderTwo   *HeaderTwo;}// header 2class CHeaderOne;  //Class prototype from header 1class CHeaderTwo{//...CHeaderOne    *HeaderOne;}// Main.cpp#include "headerone.h"#include "headertwo.h"  // now these can be defined in any order.

- Newb Programmer: Geek++

##### Share on other sites
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

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631767
• Total Posts
3002239
×