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++] Data structure and database (solved)

#109336 - Nil - Fri Nov 17, 2006 11:36 am

Hi,

I am developping a game in C++ for DS and I am stuck with an error when I build my database. I haven't programmed in C++ for several years so I am hoping that I am just making an obvious mistake (well not to me :(). Can you please help me ?

If there is a better path for declaring data in a program (a monster db, or whatever), I will happily take it (and look into source code).

So far what I have done (see source and compile result below):
- declare a class A with the data elements (a pointer to the sprite declaration, its shape and palette Id)
- built an array containing objects of the previous class (A). Ideally I want it to be const (since for that case I will not change its content). I try to declare the variable inside the class B (as private) and then instanciate it in the constructor of class (B)... but I have the same error as what I show below (where the array of A is declare outside of class B).

gfxconstants.h
Code:

#ifndef _GFXCONSTANTS_H
#define _GFXCONSTANTS_H

class _gfxfilesDB_{
   public:   
//         _gfxfilesDB_(char *pName, unsigned short shape, unsigned short palette)
//            : name(pName), shape(shape), palette(palette) {}
         char *name;      
         const unsigned short *sprData;         
         const unsigned short shape;
         const unsigned short palette;
};
#endif //_GFXCONSTANTS_H


optionscreen.h
Code:

#ifndef _OPTIONSCREEN_H
#define _OPTIONSCREEN_H

#include "constants.h"
#include "gfxconstants.h"

class OptionScreen {
   public:
      OptionScreen();
      ~OptionScreen();
      void Init();
   private:
};

#endif //_OPTIONSCREEN_H


and finally optionscreen.cpp
Code:

#include "optionscreen.h"
#include "main.h"

#define PANSPRITEPAL 0

const _gfxfilesDB_ gfxGalSize[3]= {
   {"Small", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Medium", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Large", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL}
};

OptionScreen::OptionScreen() { ... }
OptionScreen::~OptionScreen() { ... }

void OptionScreen::Init() { ... }


Output when building:
Code:

optionscreen.cpp
d:/Cpp/Nil/source/optionscreen.cpp:10: error: too many initializers for 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp:10: error: too many initializers for 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp:10: error: too many initializers for 'const _gfxfilesDB_'
make[1]: *** [optionscreen.o] Error 1
make: *** [build] Error 2


Can someone help me ?

Thanks in advance,

Nil


Last edited by Nil on Fri Nov 17, 2006 2:42 pm; edited 1 time in total

#109339 - KoshNi - Fri Nov 17, 2006 12:08 pm

Here you have 3 parameters :

Code:

_gfxfilesDB_(char *pName, unsigned short shape, unsigned short palette)
//                          1                       2                                3
            : name(pName), shape(shape), palette(palette) {} 


and here 4 :

Code:

const _gfxfilesDB_ gfxGalSize[3]= {
   {"Small", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Medium", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Large", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL}
  //     1                              2                                     3                           4                               
};

_________________
Sunshine. Beauty. Love. Happiness.

#109341 - Nil - Fri Nov 17, 2006 12:58 pm

Hi,

I have left that function in comment ( // ) just to show that I have try to do that. It is not up to date since this seems to me that it is not the way to go (I am wrong) ?
So it is not responsible for the errors I have.

#109343 - KoshNi - Fri Nov 17, 2006 1:18 pm

What happens when you uncomment the constructor and give only three parameters here :

Code:

const _gfxfilesDB_ gfxGalSize[3]= {
   {"Small", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Medium", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Large", (unsigned short *)Size_Small_Sprite, OBJ_SIZE_16X32, PANSPRITEPAL}
  //     1                              2                                     3                           4                               
};

_________________
Sunshine. Beauty. Love. Happiness.

#109344 - Nil - Fri Nov 17, 2006 1:38 pm

Ok, I have uncommented the constructor and updated the data (I removed the pointer to sprData). Here is the result:

Code:

main.cpp
optionscreen.cpp
d:/Cpp/Nil/source/optionscreen.cpp:10: error: braces around scalar initializer for type 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp:10: error: braces around scalar initializer for type 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp:10: error: braces around scalar initializer for type 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp: In constructor 'OptionScreen::OptionScreen()':
d:/Cpp/Nil/source/optionscreen.cpp:20: error: no matching function for call to '_gfxfilesDB_::_gfxfilesDB_()'
d:/Cpp/Nil/source/gfxconstants.h:6: note: candidates are: _gfxfilesDB_::_gfxfilesDB_(char*, short unsigned int, short unsigned int)
d:/Cpp/Nil/source/gfxconstants.h:4: note:                 _gfxfilesDB_::_gfxfilesDB_(const _gfxfilesDB_&)


d:/Cpp/Nil/source/optionscreen.cpp:10 is the closing brakets ("};") in :
Code:

const _gfxfilesDB_ gfxGalSize[3]= {
   {"Small", OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Medium", OBJ_SIZE_16X32, PANSPRITEPAL},
   {"Large", OBJ_SIZE_16X32, PANSPRITEPAL}
};


OBJ_SIZE_16X32 and PANSPRITEPAL are #define with value 0.

#109346 - KoshNi - Fri Nov 17, 2006 1:54 pm

Code:

d:/Cpp/Nil/source/optionscreen.cpp:10: error: braces around scalar initializer for type 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp:10: error: braces around scalar initializer for type 'const _gfxfilesDB_'
d:/Cpp/Nil/source/optionscreen.cpp:10: error: braces around scalar initializer for type 'const _gfxfilesDB_'


I googled for "braces around scalar initializer for type" and I found
http://www.koders.com/c/fid7D6C62714FB9B16764AE1F3E6CFE2E05BEF2481E.aspx line number 4345 :
Quote:
/* It is invalid to initialize a non-aggregate type with a
brace-enclosed initializer. (...)


What it means to me is that you can't give a char* a value with this kind of initialisation. Try to create your table line by line without using {}.



Code:

d:/Cpp/Nil/source/optionscreen.cpp:20: error: no matching function for call to '_gfxfilesDB_::_gfxfilesDB_()'
d:/Cpp/Nil/source/gfxconstants.h:6: note: candidates are: _gfxfilesDB_::_gfxfilesDB_(char*, short unsigned int, short unsigned int)
d:/Cpp/Nil/source/gfxconstants.h:4: note:                 _gfxfilesDB_::_gfxfilesDB_(const _gfxfilesDB_&)


It seems that a _gfxfilesDB_ is declared without using the 3 parameters constructor nor the copy constructor : either create a no parameter constructor, or use one that already exists
_________________
Sunshine. Beauty. Love. Happiness.

#109349 - Nil - Fri Nov 17, 2006 2:42 pm

Thanks for the hint to just try google on my errors .... I finally understood that somehow I must have the wrong number of parameters... in fact OBJ_SIZE_16X32 was defined in a library I was using (but not designed) and it is :
#define OBJ_SIZE_16X32 1,0
which counts as 2 parameters (I completely forgot that this kind of thing was possible ... ).

So thanks again for your time,

Nil

#109356 - Peter - Fri Nov 17, 2006 4:05 pm

Nil wrote:
in fact OBJ_SIZE_16X32 was defined in a library I was using (but not designed) and it is :
#define OBJ_SIZE_16X32 1,0
which counts as 2 parameters (I completely forgot that this kind of thing was possible ... ).

One of the most evil macro found in the HAM SDK imo, as this totally screws every code parser. Even VisualAssistX can't handle it properly when it provides its intellisense help.