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.

C/C++ > class function library include?

#155353 - nsm333 - Mon Apr 28, 2008 7:10 pm

I can't seem to compile anything with the class function. any one know what the header file for class is?

#155354 - kusma - Mon Apr 28, 2008 7:12 pm

Class function? "class" is a reserved keyword in c++, not a function. Care to share the code that causes trouble?

#155360 - silent_code - Mon Apr 28, 2008 8:20 pm

please post some short example, as there are numerous possibilities where the "error" could come from.

do you use the ".cpp" (as well as the optional ".hpp" for headers) file suffix?

how do you compile your code? via libnds example makefile, via a batch file/script or even by hand (g++)?

#155362 - nsm333 - Mon Apr 28, 2008 8:52 pm

class robot {
public:
int health;
int speed;
int tool1;
int tool2;
int tool3;
int x;
int y;
void moveup();
void movedown();
void moveleft();
void moveright();
robot::moveup() {
x+=2;
}
robot::movedown() {
x-=2;
}
robot::moveleft() {
y-=2;
}
robot::moveright() {
y+=2;
}

}

i might be doing it wrong, but i don't think so.

#155363 - kusma - Mon Apr 28, 2008 8:56 pm

nsm333 wrote:
i might be doing it wrong, but i don't think so.


You are. Try this:
Code:

class robot {
public:
   int health;
   int speed;
   int tool1;
   int tool2;
   int tool3;
   int x;
   int y;
   void moveup();
   void movedown();
   void moveleft();
   void moveright();
};

void robot::moveup() {
   x+=2;
}
void robot::movedown() {
   x-=2;
}
void robot::moveleft() {
   y-=2;
}
void robot::moveright() {
   y+=2;
}

#155364 - nsm333 - Mon Apr 28, 2008 8:58 pm

what'd you change? but, it didn't work. and, here's the whole code
Code:

#include <nds.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
class robot {
public:
   int health;
   int speed;
   int tool1;
   int tool2;
   int tool3;
   int x;
   int y;
   void moveup();
   void movedown();
   void moveleft();
   void moveright();
};

void robot::moveup() {
   x+=2;
}
void robot::movedown() {
   x-=2;
}
void robot::moveleft() {
   y-=2;
}
void robot::moveright() {
   y+=2;
}

}



robot player1;

   while(1) {

      uint16 keysPressed = ~(REG_KEYINPUT);

    if(keysPressed & KEY_UP)
      player1.moveup();
    if(keysPressed & KEY_DOWN)
      player1.movedown();
    if(keysPressed & KEY_LEFT)
      player1.moveleft();
    if(keysPressed & KEY_RIGHT)
      player1.moveright();

      
      iprintf("Robot x = %d/nRobot y = %d", player1.x, player1.y);

      swiWaitForVBlank();
   }


   return 0;

and the errors:
Code:

> "make"
main.c
arm-eabi-gcc -MMD -MP -MF /c/ndsdev/arm9/build/main.d -g -Wall -O2 -march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork -I/c/ndsdev/arm9/include -I/c/devkitPro/libnds/include -I/c/devkitPro/libnds/include -I/c/ndsdev/arm9/build -DARM9 -c /c/ndsdev/arm9/source/main.c -o main.o
c:/ndsdev/arm9/source/main.c: In function 'main':
c:/ndsdev/arm9/source/main.c:8: error: 'class' undeclared (first use in this function)
c:/ndsdev/arm9/source/main.c:8: error: (Each undeclared identifier is reported only once
c:/ndsdev/arm9/source/main.c:8: error: for each function it appears in.)
c:/ndsdev/arm9/source/main.c:8: error: expected ';' before 'robot'
c:/ndsdev/arm9/source/main.c:23: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
c:/ndsdev/arm9/source/main.c:23: error: expected expression before ':' token
make[1]: *** [main.o] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01

#155365 - Dwedit - Mon Apr 28, 2008 9:04 pm

you can't define a class inside a function
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#155366 - nsm333 - Mon Apr 28, 2008 9:05 pm

dur de dur. thanks, guys.

ok, outside of the function now, but same thing. i'll try to fix this myself, though.

ok, just some semi-colons out of place. all fixed.

#155367 - PeterM - Mon Apr 28, 2008 9:13 pm

nsm333 wrote:
and the errors:
Code:

> "make"
main.c
...

I'm pretty sure you can declare a class inside a function if you like. The real problem is that you're compiling C++ code as C.

Try renaming your file to main.cpp and editing your Makefile accordingly, then see if that helps!

Pete
_________________
http://aaiiee.wordpress.com/

#155368 - nsm333 - Mon Apr 28, 2008 9:19 pm

ok, i'll try that. just useing the example file with devkit arm.

edit: it worked! thanks.


Last edited by nsm333 on Mon Apr 28, 2008 9:22 pm; edited 1 time in total

#155369 - kusma - Mon Apr 28, 2008 9:22 pm

nsm333 wrote:
what'd you change?

I moved the implementations of the class-methods out of the class-definition, and added the return-type. And then I added a semicolon after the class-definition.

#155370 - nsm333 - Mon Apr 28, 2008 9:23 pm

oh, but as peterm said, it probaly can be inside. well, thanks guys. it compiled fine, and now i need to set up the screen....

edit:
silent_code wrote:

do you use the ".cpp" (as well as the optional ".hpp" for headers) file suffix?
sorry, silent_code, you were right.

#155373 - kusma - Mon Apr 28, 2008 9:56 pm

nsm333 wrote:
oh, but as peterm said, it probaly can be inside.

No, he said the class definition could be inside a function, which is correct. The method-implementations can also be inside the class-definition, but not with the syntax you used.

This works:
Code:
class SomeClass
{
void someFunc() { printf("hello"); }
};

This works:
Code:
class SomeClass
{
void someFunc();
};
void SomeClass::someFunc() { printf("hello"); }

This was pretty much what you did, and it does not work:
Code:
class SomeClass
{
void someFunc();
SomeClass::someFunc() { printf("hello"); }
};

The key point is that "SomeClass::" is used to specify namespace outside of the class definition. You also lacked the return-type (in this case, "void").

#155374 - silent_code - Mon Apr 28, 2008 10:01 pm

no problem! :^)

happy coding! :^D

#155449 - nsm333 - Tue Apr 29, 2008 6:27 pm

ok, kusuma. i get it now. thanks.