C++ trouble with OOP

Started by
8 comments, last by p_mxv 20 years, 3 months ago
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
Advertisement
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
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
In the file "CScheduler.h", check to see if you are missing a semicolon at end of class declaration.
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!
try moving the CScheduler.h include further up in that list of includes.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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]
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.


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

- Newb Programmer: Geek++
- Newb Programmer: Geek++
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

This topic is closed to new replies.

Advertisement