4

I'm writing a class for the Arduino. It's been going well so far, but I'm sort of stuck now...

I have declared an int array in my class

class myClass
{
  public: MyClass(int size);
  private:
    int _intArray[];
};

When I initialize the class MyClass myClass1(5) I need the array to look like this {0,0,0,0,0}.

My question: what do I need to do so that the array contains 'size' amount of zeros?

MyClass::MyClass(int size)
{
    //what goes here to dynamically initialize the array
    for(int i=0; i < size; i++) _intArray[i] = 0;
}

Edit: Following up on various replies below, Arduino does not include the standard library so unfortunately std::vector is not an option

0

5 Answers 5

2

Your code as I'm writing this:

class myClass
{
  public: MyClass(int size);
  private:
    int _intArray[];
};

The declaration of _intArray is not valid C++: a raw array needs to have a size specified at compile time.

You can instead instead use a std::vector:

class myClass
{
public:
    MyClass( int size )
        : intArray_( size )    // Vector of given size with zero-valued elements.
    {}

private:
    std::vector<int> intArray_;
};

Note 1: some compilers may allow your original code as a language extension, in order to support the "struct hack" (that's a C technique that's not necessary in C++).

Note 2: I've changed the name of your member. Generally underscores at the start of names can be problematic because they may conflict with names from the C++ implementation.

Cheers & hth.,

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

7 Comments

I get an error saying "ISO C++ forbids declaration of 'vector' with no type" ledLib.h:40: error: ISO C++ forbids declaration of 'vector' with no type MyClass.h:40: error: invalid use of '::' MyClass.h:40: error: expected ';' before '<' token MyClass.h: In constructor 'MyClass::MyClass(int)': MyClass.h:36: error: class 'MyClass' does not have any field named 'intArray_'
@JNK: have you included <vector> header?
I included it (now) but it doesn't change anything...:(
I found a work-around by simply passing an array to the class and using the pointer later on... not perfect but it works
@JNK: using a raw array has many problems for the novice. if you want to pursue that line then read up on the "rule of three". the simplest is to get the std::vector working -- it should not be a problem (it works for everyone else).
|
2

You should use a std::vector.

class myCLass {
public:
    myClass(int size)
        : intarray(size) {
    for(int i = 0; i < size; i++) intarray[i] = 0;
    }
private:
    std::vector<int> intarray;
};

3 Comments

why not ": intarray(size,0) {" ?
@Oxsnarder: Meh, not really that versed in vector's constructors.
The zeroing loop is not ncessary: std::vector guarantees to zero those elements.
2

You should really use vectors as others have suggested. A work-around could be as shown (in case you do not want to use memcpy or a loop).

This would be useful if you have a really huge array. Note that it would add a level of indirection to access the array.

class myClass 
{ 
public: 
   myClass(){
      mt = T();    // value initialize.
   }
private:
   struct T{
      int _intArray[10]; 
   } mt;
};

int main(){
   myClass m;
}

Comments

2

I'll try the following:

class myClass
{
  public: 
    MyClass(int size);
    ~MyClass();
  private:
    int* _intArray;
};

MyClass::MyClass(int size) {
  _intArray = new int[size];
  for (int i=0; i<size; ++i) _intArray[i] =0; // or use memset ...
}

MyClass::~MyClass() {
  delete[] _intArray;
}

Or, even better, use a STL vector instead ...

Comments

0

you can use another hack basing on a string value and then populate a limited size array check this : https://github.com/Riadam/ViewPort-Array-Shifter-for-Arduino-Uno.git

1 Comment

I think I've seen another of your posts... where you got asked to inline your code in case the linked resource rots.

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.