• ### What is your GameDev Story?

#### Archived

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

# Class Problem

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

## Recommended Posts

Hi, I''m trying to get a class up and working in my game but it does''t realise I''m trying to make a class, and I get errors when I compile. Here is the code if anyone knows why, it would help me greatly, (its in file called carc.h)
  #ifndef CARC_H #define CARC_H class Car { public: int xpos; int ypos; int ang; int incr; float vx; float vy; float v; float rotate_ang; void control_loop(); void movement_loop(); }; #endif 

##### Share on other sites
looks good I think, should work in VisualC++. I had CodeWarrior from earlier and shockingly it couldn''t handle classes in headers. Maybe that''s your problem.

##### Share on other sites
I''m trying to compile it in DJGPP compiler, and I tried compile it in my main source code and that would work either. So someone told me to try and put it in a header file.

##### Share on other sites
What you have is fine. Since you have all the members of your class defined as public, it''s a little shorter to make the thing a struct instead. In C++, the only difference between a class and a struct is that a class''s default member access is private, while struct''s is public.

So, now we get to the problem. Again, what you have looks just fine. I assume that you''re implementing that class in some source file named, for example, carc.cc (for all GCC-derived compilers such as DJGPP running on a case-insensitive file system like you have in DOS and Windows, the conventional file extension for C++ source files is .cc):

  // carc.cc#include void Car::control_loop(){ ...}void Car::movement_loop(){ ...}

Whether or not you already know how to implement classes, I described it there just to cover bases, so don''t be offended. ; That should do the trick. I''m sorry I can''t be more helpful at the moment. If you continue to have problems, tell us which error messages DJGPP gives you.

##### Share on other sites
what compiler are you using ??

visual C++
------------
  #if !defined(CARC_H) #define CARC_H #endif

sounds silly, but that cause me problem in the past.

{ Stating the obvious never helped any situation !! }

##### Share on other sites
quote:
Original post by FlorianapoliS
I''m trying to compile it in DJGPP compiler, and I tried compile it in my main source code and that would work either. So someone told me to try and put it in a header file.

You try to compile it with gcc?
try gxx (I think... It''s been a long time since i last used DJGPP).

/JanneVee
"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++

##### Share on other sites
Thanks for all your help, but I still can''t get it to work. I tried using gxx but that didn''t work either.

Here are the errors I get when I compile:

In file included from C:\M.z\game\car.c:6:
C:/M.z/game/carc.h:8: parse error before :''
C:/M.z/game/carc.h:55: parse error before :''
C:\M.z\game\car.c:21: parse error before Car''
C:\M.z\game\car.c:22: syntax error before {''
C:\M.z\game\car.c:35: parse error before }''
C:\M.z\game\car.c: In function main'':
C:\M.z\game\car.c:42: Car'' undeclared (first use in this function)
C:\M.z\game\car.c:42: (Each undeclared identifier is reported only onc
C:\M.z\game\car.c:42: for each function it appears in.)
C:\M.z\game\car.c:42: parse error before Player''
C:\M.z\game\car.c:46: `Player'' undeclared (first use in this function)

Car is the name of the class
Player is an object of type Car eg. Car Player;

##### Share on other sites
...and u linked it with stdcxx.a library when u compiled with gxx? ex. gxx -o foo.exe foo.cc -lstdcxx

##### Share on other sites
C sources are compiled with gcc. C++ sources are compiled with gxx.

It concerns me that your C++ source file has a .c extension. That signifies that the source file contains C code, when it really contains C++ code. So, I''d suggest that you make sure your C++ source files end in .cc (or .cpp, but that goes against the GCC convention). That shouldn''t be the problem, but it''s something you should be aware of.

I think that the only way we''ll be able to help you is if we see your actual source.

##### Share on other sites
You should rename car.c to car.cpp or car.cc. The compiler in DJGPP (gcc) determines the source language by looking at the extension of the filename. If it's .c, the file will be compiled as C. If it's .cpp or .cc, it will be compiled as C++. On DOS/Windows, .cpp is the most common extension for C++ files. On Unix and Linux, .cc is more common.

Using gxx doesn't really help: it links the C++ libraries (for iostream for example) with the object files. If you use gcc, you have to specify those libraries yourself by using the -l commandline option.

Judging from the error messages you posted, it seems the file is indeed compiled as a C source file.

HTH

Edited by - JungleJim on July 19, 2001 1:57:29 PM

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015213
×