#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:
also, do you think this is a good way of implementing a boolean array or are there better ways?
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?