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++ > error with global objects

#168859 - vuurrobin - Sun May 31, 2009 4:56 pm

hello everybody

I had a problem with some code. after a while I figured out how to solve it, but I dont understand why it would solve the problem, or why it was wrong in the first place.

I created a global object of a (template) class, and initialised it by putting '()' right after the objects name. after that I tried to create a temporarily object by using '=', but the compiler gave me an 'error: invalid initialization of reference of type 'BooleanArray16&' from expression of type 'BooleanArray16 ()()''. if I remove the '()' from the global object decleration, the error go's away. why? AFAIK, both with and without the '()' it should call the standard constructor, so why does it make a difference?

here is the code if anyone wants to see it:

Code:
typedef unsigned int uint;

typedef unsigned char       u8;
typedef unsigned short int  u16;
typedef unsigned long int   u32;
typedef signed char         s8;
typedef signed short int    s16;
typedef signed long int     s32;


//#define BIT(x) (1 << (x))

inline int BIT(int x)
{
    return 1<<x;
}


/**
    @brief a class for creating a static(with a fixed length) boolean array.
*/
template <typename T>
class BooleanArray
{
    private:
        //a user defined variable, the bits of the variable will be used as an array of booleans
        T array;

    public:
        /**
            @brief creates the array and fills the booleans with false
        */
        BooleanArray() : array(0)
        {}

        /**
            @brief returns the value at the given index

            @param index the index you want to get

            @return the value at the given index
        */
        bool get(const uint index) const
        {
            //sassert(index < (sizeof(T) * 8), "BooleanArray get() index out of bounds");

            return ((array & BIT(index)) != 0);
        }

        /**
            @brief sets the given value at the given index

            @param index the index of the array that you want to change
            @param value the value that the array at position index should become
        */
        void set(const uint index, const bool value)
        {
            //sassert(index < (sizeof(T) * 8), "BooleanArray set() index out of bounds");

            if(value)
            {
                //sets the bit at index to 1
                array |= BIT(index);
            }
            else
            {
                //sets the bit at index to 0
                array &= ~BIT(index);
            }
        }

};//end class BooleanArray

/** @brief an typedef for the BooleanArray with the size already filled in */
typedef BooleanArray<u8> BooleanArray8;
/** @brief an typedef for the BooleanArray with the size already filled in */
typedef BooleanArray<u16> BooleanArray16;
/** @brief an typedef for the BooleanArray with the size already filled in */
typedef BooleanArray<u32> BooleanArray32;



BooleanArray16 test();// = BooleanArray16();
BooleanArray16 test2();

int main()
{
    register BooleanArray16& temp = test;

    temp = test2;

    return 0;
}


also, do you think this is a good way of implementing a boolean array or are there better ways?

#168860 - Tyler24 - Mon Jun 01, 2009 2:05 am

I'm confused as to why you think it's supposed to be BooleanArray()()... I've never seen ()() before. What would it mean, if it could mean something?

I'm not all too familiar with C/C++ syntax, but wouldn't you want something like:

Code:
BooleanArray *boolArray = new BooleanArray();

#168863 - gauauu - Mon Jun 01, 2009 2:53 am

My C++ isn't really that strong, so somebody smart should correct me if I misspeak. That being said, there are (at least) two ways to allocate an object:

First, using new and a pointer:
Code:
BooleanArray *boolArray = new BooleanArray();


Second, just declaring the object:
Code:
BooleanArray boolArray;


This second way generally puts the object on the stack instead of in the heap (if you do it within a function). Doing it for a global, I believe, will put it in the heap.

#168864 - Dwedit - Mon Jun 01, 2009 3:07 am

Globals aren't actually in the heap, they go in the BSS section.
So the class instance for a variable sized array is sitting in the BSS section (probably in IWRAM/DTCM), and the DATA for the variable sized array is in the heap.

I think you might be declaring 'test' as a function that takes in no parameters. Take the parenthesis off the variable names, put them back on the end of the default constructor class name.

like "BooleanArray16 test = BooleanArray16(); "
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#168869 - vuurrobin - Mon Jun 01, 2009 12:00 pm

wow, never realised that it looked so much like a function decleration.

thank everybody :).