3

Here's a weird issue I'm facing - probably something ultra-basic given my rusty C++ skills but I'm still perplexed :

  • I've got a class
  • We've also got an array of unsigned long longs in this class - let's call that arr

My Class Interface :

typedef unsigned long long U64;

class DQClass
{
    public:
        DQClass (void);
        virtual ~DQClass (void);

        U64 arr[12];
};

Now as for the implementation...

Test 1 (This works) :

DQClass::DQClass (void)
{
        this->arr[0] = 0x8100000000000000ULL;
        this->arr[1] = 0x4200000000000000ULL;

        // and so on..
}

Test 2 (This doesn't) :

DQClass::DQClass (void)
{
    this->arr = 
    {
        0x8100000000000000ULL,
        0x4200000000000000ULL,
        0x2400000000000000ULL,
        0x1000000000000000ULL,
        0x0800000000000000ULL,
        0x00FF000000000000ULL,
        FLIPV(0x8100000000000000ULL),
        FLIPV(0x4200000000000000ULL),
        FLIPV(0x2400000000000000ULL),
        FLIPV(0x1000000000000000ULL),
        FLIPV(0x0800000000000000ULL),
        FLIPV(0x00FF000000000000ULL)
    };
}

Error :

dqclass.cpp: In constructor ‘DQClass::DQClass()’:
dqclass.cpp:28: error: expected primary-expression before ‘{’ token
dqclass.cpp:28: error: expected `;' before ‘{’ token

Why isn't this working? Shouldn't it be working in the same fashion as, e.g. U64 someArr[12] = {0,1,2,3,4,5,6,7,8,9,10,11} would?

Any ideas?

3
  • what if you move the { to the same line as the = ? Commented Dec 13, 2012 at 8:19
  • 1
    @NahumLitvin I would be really surprised if something like that worked... And nope, it doesn't. Commented Dec 13, 2012 at 8:21
  • Arrays are broken in C++ (because they are broken in C). That's why things like std::vector and std::array were invented. Commented Dec 13, 2012 at 8:23

1 Answer 1

6

Arrays can't be assigned like that (or any other way), only initialized:

 // sorry for bad formatting
DQClass::DQClass (void)
: arr(
    {
        0x8100000000000000ULL,
        0x4200000000000000ULL,
        0x2400000000000000ULL,
        0x1000000000000000ULL,
        0x0800000000000000ULL,
        0x00FF000000000000ULL,
        FLIPV(0x8100000000000000ULL),
        FLIPV(0x4200000000000000ULL),
        FLIPV(0x2400000000000000ULL),
        FLIPV(0x1000000000000000ULL),
        FLIPV(0x0800000000000000ULL),
        FLIPV(0x00FF000000000000ULL)
    }) {
}

Use constructor initialize list.


You could also use std::array:

std::array<U64, 12> arr;

// ...

this->arr = 
    {{
        0x8100000000000000ULL,
        0x4200000000000000ULL,
        0x2400000000000000ULL,
        0x1000000000000000ULL,
        0x0800000000000000ULL,
        0x00FF000000000000ULL,
        FLIPV(0x8100000000000000ULL),
        FLIPV(0x4200000000000000ULL),
        FLIPV(0x2400000000000000ULL),
        FLIPV(0x1000000000000000ULL),
        FLIPV(0x0800000000000000ULL),
        FLIPV(0x00FF000000000000ULL)
    }};
Sign up to request clarification or add additional context in comments.

12 Comments

I'm not really sure what you're trying to say in your first piece of code... Could please explain to me why U64 someArr[12] = {0,1,2,3,4,5,6,7,8,9,10,11} does work?
@Dr.Kameleon Initialization is different from assignment. Arrays have silly behavior and so I can't give you a good reason why arrays can't be assigned. Just use std::array for such purposes.
@Dr.Kameleon Maybe this will help stackoverflow.com/questions/3437110/…
OK, here's another interesting issue : I decided to try the std::array way. While trying #include <array>, I'm getting error: array: No such file or directory and ISO C++ forbids declaration of ‘array’ with no type. (Hint : using g++ in Mac OS X 1.6.8)
@Dr.Kameleon You need a fairly recent version that supports C++11. You could also try boost::array which is what std::array is based off of.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.