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++ > C++ classes - quick help

#69412 - justatest - Mon Jan 30, 2006 5:50 pm

Hey,

I'm making the transition from C to C++, and am having problems compiling. Loads of syntax errors :-(

Here's a class:

Code:
class MyClassName {
   private:
      int an_integer;
      
      int another_var;
      
   public:
      void FunctionOne(void) {
         an_integer++;
      }
}


A Makefile:

Code:
@echo off

set CFILES= main.c
set OFILES= main.o
set GAME= main
set SPECS=gba_mb.specs

set DEVDIR=C:\Devkitarm

PATH=%DEVDIR%\bin;%path%

set LIBDIR= %DEVDIR%\arm-elf\lib\interwork
set LIBDIR2= %DEVDIR%\libgba\lib

set INCDIR= %DEVDIR%\arm-elf\include
set INCDIR2= ..\..\include

set CFLAGS= -I.  -I%INCDIR% -I%INCDIR2% -c -g -O2 -Wall -mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork

set LDFLAGS= -mthumb -mthumb-interwork -specs=%SPECS% -L%LIBDIR% -L%LIBDIR2% 

arm-elf-gcc  %CFILES% %CFLAGS%
arm-elf-gcc -o %GAME%.elf  %OFILES% %LDFLAGS%

arm-elf-objcopy -v -O binary %game%.elf %game%.gba

gbafix %game%.gba

del %OFILES%
del %game%.elf

..\..\emulators\VisualBoyAdvance.exe %game%.gba


Compilation errors:

Code:
In file included from main.c:32:
class_mario.cpp(1): error: parse error before "MyClassName"
class_mario.cpp(1): error: syntax error before '{' token
class_mario.cpp(18): error: parse error before ':' token
etc etc..


What the hell am I doing wrong? Do I need extra command line paremeters?

#69421 - keldon - Mon Jan 30, 2006 6:36 pm

Rename files as .cpp. Use g++ not gcc (I think CC is only C Compiler). Use semicolon at end of classes.

#69454 - YopYop - Mon Jan 30, 2006 9:14 pm

Code:

class MyClassName {
   private:
      int an_integer;
     
      int another_var;
     
   public:
      void FunctionOne(void) {
         an_integer++;
      }
}; <<HERE

I didn't read evry thing but you must put a ; at the end of the class declaration.

yopyop

#69461 - wintermute - Mon Jan 30, 2006 10:01 pm

justatest wrote:


A Makefile:


No it isn't, this is a batch file.

Don't ever do this

Code:

set LIBDIR= %DEVDIR%\arm-elf\lib\interwork


1. It doesn't match the libraries you're telling gcc to link
2. Should you have occasion to use non interworking code then you'll spend an hour or two trying to figure out why it won't link.
3. Using gcc or g++ to link already takes care of the proper library paths.

or this

Code:

set INCDIR= %DEVDIR%\arm-elf\include


The compiler knows where it's include files are, you don't need to tell it.

#101395 - cppdungeon - Tue Sep 05, 2006 12:55 am

dont forget class constructors/destructors
_________________
"in Soviet Russia, car pimps you!"

#101541 - sgeos - Wed Sep 06, 2006 10:29 am

If you are getting syntax errors, I don't think you are ready for the GBA yet. Take things one step at a time.

Read a C++ and spend a couple of days doing command line work on the PC. Read the entire comp.lang.c++ FAQ. Move to the GBA when you feel you are ready.

-Brendan

#101581 - sajiimori - Wed Sep 06, 2006 6:38 pm

Brendan, in case you didn't read the thread, the main cause of the errors was using the gcc front end instead of g++. I've made the same mistake myself plenty of times.

Anyway, I still get syntax errors even after shipping a few games. C++ just isn't one of those languages where you learn the syntax at the beginning and never worry about it again. ;)

#101650 - sgeos - Thu Sep 07, 2006 12:31 am

sajiimori wrote:
Brendan, in case you didn't read the thread,

Unless I misread the thread, the original poster made multiple errors. Two errors at the same time are much more confusing than just one. Even if you solve one of them, you may break it again if you don't know what you are doing. Effects escalate with more errors. In the original poster's case, half of his mistakes could have been resolved by feet wetting terminal programming.

sajiimori wrote:
the main cause of the errors was using the gcc front end instead of g++. I've made the same mistake myself plenty of times.

He could (would) have made the same mistakes if he spent a couple of days doing terminal programming. Admittedly, the library linkage is GBA specific- this compounds the problem. The reason why I recommend a couple of days of terminal programming when transitioning to a new language.

sajiimori wrote:
Anyway, I still get syntax errors even after shipping a few games.

Everybody gets syntax errors. 99% of the time you should be able to figure them out on your own. I'll even go so far as to argue that that is part of learning the language.

sajiimori wrote:
C++ just isn't one of those languages where you learn the syntax at the beginning and never worry about it again. ;)

Perhaps not. Read above for my thoughts on this. =)

-Brendan

#101656 - sajiimori - Thu Sep 07, 2006 1:51 am

There was one typo. But whatever, I'm not going to debate it further.