-1

I'm writing Saleae Custom Analyzer but I am also new to C++
This does not work inside of a class: Declare array in C++ header and define it in cpp file?
How to do it in a class?

class SimpleSerialSimulationDataGenerator
{
public:
    SimpleSerialSimulationDataGenerator();
    ~SimpleSerialSimulationDataGenerator();

    void Initialize( U32 simulation_sample_rate, SimpleSerialAnalyzerSettings* settings );
    U32 GenerateSimulationData( U64 newest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channel );

protected:
    SimpleSerialAnalyzerSettings* mSettings;
    U32 mSimulationSampleRateHz;

protected:
    void CreateSerialByte();
    U8 mSerialText[3] = {0xAA, 0x01, 0x55};
    U32 mStringIndex = 0;

    SimulationChannelDescriptor mSerialSimulationData;

};

mSerialText is what I would like to init below in .cpp not in header:

SimpleSerialSimulationDataGenerator::SimpleSerialSimulationDataGenerator()
{
    mSerialText = {0xAA, 0x01, 0x55};
}

but in cpp it says 'must be lvalue'
1.Can I change length later on?
2.Can I change value later on?
My dream solution is to declare array in header file with no length and init in cpp file with random data.

6
  • 7
    std::vector Commented Feb 20, 2018 at 15:44
  • 2
    I get what you're getting at, @NathanOliver, but only because I understand C++, whereas Lukasz is a self-admittedly new to C++ and I think your answer is too brief. So perhaps you could actually provide a little bit of text with your answer ... and maybe even write it as an answer rather than a comment. Commented Feb 20, 2018 at 15:48
  • 1) Did you try initializing array in constructor initializer list? Example. 2) "Can I change length later on?" Of an array? No. 3) "Can I change value later on?" Sure, you can manipulate the data in the array, in any way you want. 4) Consider learning from a good C++ book. Commented Feb 20, 2018 at 15:52
  • Prefer std::array. Commented Feb 20, 2018 at 15:59
  • @Wyck: You are absolutely right. On the other hand, Lukasz should read his C++ book; repeating the very basics of the language 50 times a day is not what any of us is here for. Commented Feb 20, 2018 at 16:03

1 Answer 1

3

For variable length array in C++ use std::vector.

In your header file you will have this:

std::vector<U8> mSerialText;

And then in source file you can initialize it with {} syntax just like in your example like this:

mSerialText = {0xAA, 0x01, 0x55};

It has an index access with operator[] or at function with out of range checks. To insert new elements, call push_back. You can read further information about how to use vector on here

Sign up to request clarification or add additional context in comments.

6 Comments

This just works with std=c++11, otherwise extended initializer lists only available with -std=c++11 or -std=gnu++11
He haven't said anywhere that he needs to use old version of c++.
But it is tagged with C++ only. Anyway, this answer is the way to go! Otherwise, the answer would be use c++11 and then follow this answer.
Since he said he is new to C++, I don't think he even knows what c++11 means. Also, c++11 or even c++14 is nowadays used by default if you don't change compile settings in f.e. visual studio.
@MarošBeťko: That's pretty short-sighted; there are plenty of OSs out there on which e.g. GCC 4.4 is still the default, which doesn't even have C++11 or C++14. If the OP isn't aware of these distinctions, even more reason to explain them.
|

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.