-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.

Lukasz
  • 152
  • 3
  • 16
  • 7
    [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) – NathanOliver Feb 20 '18 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. – Wyck Feb 20 '18 at 15:48
  • 1) Did you try initializing array in constructor initializer list? [Example](https://ideone.com/OkKFPI). 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](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Feb 20 '18 at 15:52
  • Prefer [std::array](http://en.cppreference.com/w/cpp/container/array). – Jesper Juhl Feb 20 '18 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. – Lightness Races in Orbit Feb 20 '18 at 16:03
  • Well I have read Quick Guide on Tutorialsoint but it is not really descriptive about vectors. Nevermind, I should have added: If annoyed by newbies then don't read. If You don't know how to ask google about a task how then find a solution? ->stack – Lukasz Feb 20 '18 at 18:13

1 Answers1

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

Maroš Beťko
  • 2,181
  • 2
  • 16
  • 42
  • 1
    This just works with `std=c++11`, otherwise `extended initializer lists only available with -std=c++11 or -std=gnu++11` – Patwie Feb 20 '18 at 15:50
  • 3
    He haven't said anywhere that he needs to use old version of c++. – Maroš Beťko Feb 20 '18 at 15:54
  • 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. – Patwie Feb 20 '18 at 16:01
  • 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 Feb 20 '18 at 16:05
  • 1
    @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. – Lightness Races in Orbit Feb 20 '18 at 16:28
  • Thank You for the answer and understanding. Everyday I use python, sometimes C#, rarely C embedded. I was looking for array and lists in search engine, never thought of vector. C++ reminds me of embedded C. – Lukasz Feb 20 '18 at 18:10