gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Compiling C++

#105624 - MattC - Tue Oct 10, 2006 8:12 am

Hi everyone,

I'm just getting into DS Dev. I started a little deving on the PSP, but decided that the game I've always wanted to create would really be much more appropriate on the DS than any other platform. However, it I don't want to get into a procedural nightmare (especially since I don't know the first thing about structured programming on a large scale), so I would like to create the project with an obeject oriented langauge. Having said that, I know a ton about Java (thats where my professional career has lead me so far), but I'm not so talented with C++.

I'm simply trying to make a class, but I'm having problems compiling it. It gets stuck on the "class" token, with this error:
Code:

In file included from c:/devkitPro/projects/carmod/source/main.c:11:
c:/devkitPro/projects/carmod/source/Sprite.h:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Sprite'


I don't see how my .h file is syntactically wrong, so I'm assuming it has to do with my makefile or something. I tried to parse through the makefile, but I'm pretty lost. It looks like its compiling with g++, but I'm not really sure.

I'm using Eclipse, but that shouldn't make any difference since its just calling "make"

Thanks,
-Matt

#105627 - felix123 - Tue Oct 10, 2006 9:52 am

Sorry if I am wrong, but are you trying to put C++ code in a .c file?

#105628 - Dark Knight ez - Tue Oct 10, 2006 10:00 am

Rename the .c files to .cpp, which are C++ files.

#105641 - tepples - Tue Oct 10, 2006 2:14 pm

MattC wrote:
However, it I don't want to get into a procedural nightmare (especially since I don't know the first thing about structured programming on a large scale), so I would like to create the project with an obeject oriented langauge.

It's possible to do object-oriented programming in a structured programming language. C++ classes are just C structs with automatic method name mangling and automatic jump table construction.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#105659 - OOPMan - Tue Oct 10, 2006 5:33 pm

Yeahm C++ is not a "pure" OO language, which is one of its strengths and also one of its weaknesses :-)
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#105664 - sajiimori - Tue Oct 10, 2006 6:29 pm

I'd say strength but not weakness. :)
Quote:
C++ classes are just C structs with automatic method name mangling and automatic jump table construction.
And constructors and destructors and copy constructors and operator overloading and automatic conversions and template methods and access control and...

#105671 - MattC - Tue Oct 10, 2006 8:41 pm

Wow, yeah, that was dumb. I knew it would come down to something dumb. I was using a template that assumed I would be using C, but I was trying to expand it with C++ classes. I guess it was a little late last night. It compiles now, thanks.

-Matt

#105691 - MattC - Wed Oct 11, 2006 2:27 am

Now I'm having problems with a static member variable. I'm trying to create a Singleton class to manage sprites, but the compiler refuses to acknowledge the existance of static variables. Every time I try to access them within the class, I get an error similar to:

Code:
error: 'i' is not a member of 'SpriteNumberManager'


I'm not incredibly great with C++ syntax, but I've looked up the examples of the singleton pattern on other websites (http://www.inquiry.com/techtips/cpp_pro/10min/10min0200.asp), and I'm implementing it in exactly the same way, but I get errors.

I'm also getting this error when trying to use NULL in my code:

Code:
error: 'NULL' was not declared in this scope


Which makes me think theres something wrong with my makefile. My structure where this class lies is:

source\
-------main.cpp
-------lib\
-----------gfx\
---------------(lib source files)

Where the lib source files are the files giving me problems. The only thing I've added to the makefile is by "SOURCES :=" I added source/lib/gfx

-Matt


Last edited by MattC on Wed Oct 11, 2006 2:43 am; edited 3 times in total

#105692 - sajiimori - Wed Oct 11, 2006 2:33 am

Code:

struct S
{
  static int i;
  int test() { return i; }
};

int S::i;

#105767 - Izhido - Wed Oct 11, 2006 8:03 pm

In C++, NULL is a constant defined as exactly 0 (zero). It is not part of the language per se; NULL is defined on <stdlib.h> (or, if you prefer, <cstdlib> ). If you want, you can use 0 instead of NULL. It's just a matter of preferences. Mine's using 0. :)

#105919 - MattC - Fri Oct 13, 2006 3:47 am

Thanks Izhido, I never knew that, I always thought it was part of the syntax.

sajiimori - are you saying I have to redefine the variable as a global variable (is that the correct scope?) as in your example to reference the static variable? Why is that...if it isn't too much explanation?

Thanks for being patient with me, this is one of the reasons I wanted to write a game for a console, since I would be forced to use C/C++. I really want to know more about the language since I haven't used it that much.

-Matt

#105970 - sajiimori - Fri Oct 13, 2006 6:53 pm

When you want to create a class-static variable, you declare it in the class body, then define it once somewhere in a .cpp file. It's just like how you create global variables (with cross-module access) by declaring them 'extern' in a header and defining them once in a .c or .cpp file (which is something that's good to learn before using class-static variables).