4

I was coding a class for "a kind of gun". I have this code for the classes:
Gun.h:

#ifndef Gun_h
#define Gun_h
#include "Arduino.h"
class Gun
{
 public:
 Gun(String identifier, String name, int teamIRCodes[]);
 String getIdentifier();
 String getName();
 int getIRCode();
 private:
 String _identifier;
 String _name;
 int _teamIRCodes[];
};
#endif

Gun.cpp:

#include "Arduino.h"
#include "Gun.h"
Gun::Gun(String identifier, String name, int teamIRCodes[])
{
 _identifier = identifier;
 _name = name;
 _teamIRCodes = teamIRCodes;
}
String Gun::getIdentifier(){
 return _identifier;
}
String Gun::getName(){
 return _name;
}
int Gun::getIRCode(){
 return _teamIRCodes[1];
}

It returns me this error while compiling:

sketch\Gun.cpp: In constructor 'Gun::Gun(String, String, int*)':
Gun.cpp:8: error: incompatible types in assignment of 'int*' to 'int [0]'
 _teamIRCodes = teamIRCodes;
 ^
exit status 1
incompatible types in assignment of 'int*' to 'int [0]'
asked May 1, 2017 at 18:31

2 Answers 2

1

The issue is with flexible array int _teamIRCodes[]; This is an old technique which I don't recommend. But if you want to use it, you need you use malloc to create a class object, adding on enough space for the array:

// Allocate space for the class AND the array
Gun *gun = (Gun *)malloc(sizeof(Gun) + sizeof(teamIRCodes));

However, this does not call the ctor, and you still have to manually copy the data:

for(int i = 0; i < (sizeof(teamIRCodes) / sizeof(teamIRCodes[0])); i++) {
 gun->_teamIRCodes[i] = teamIRCodes[i];
}

I think it's much simpler to use a pointer:

int* _teamIRCodes;

Then you can make your ctor:

Gun(String identifier, String name, int teamIRCodes[], int numberOfCodes) {
 _teamIRCodes = new int[numberOfCodes];
 // memcpy does the same thing as the for loop - copies the array
 memcpy(_teamIRCodes, teamIRCodes, sizeof(teamIRCodes[0]) * numberOfCodes);
}

And don' forget to add a dtor:

~Gun() {
 delete [] _teamIRCodes;
}

Or, if teamIRCodes is persistent throughout the lifetime of the object, you can just use a pointer to it:

 class Gun {
 public:
 int *_teamIRCodes;
 int _numberOfCodes;
 Gun(String identifier, String name, int teamIRCodes[], int numberOfCodes) {
 _teamIRCodes = teamIRCodes;
 _numberOfCodes = numberOfCodes;
 };

(Maybe this is an Arduino thing as this is the 2nd flexible array question I've seen today).

answered May 1, 2017 at 19:20
0

Ok, using this worked:
Replace

_teamIRCodes = teamIRCodes;

For

for(int i = 0; i < (sizeof(teamIRCodes)/sizeof(int)) ;i++)
 _teamIRCodes[i] = teamIRCodes[i];

Needed to iterate through array, cannot copy full array.

answered May 1, 2017 at 18:44
4
  • Close, but where do you allocate space for _teamIRCodes? Commented May 1, 2017 at 18:49
  • Doesn't it initialize with a value of 0? In Gun::Gun(String identifier, String name, int teamIRCodes[]) I add all the values Commented May 1, 2017 at 18:52
  • By now, it worked Commented May 1, 2017 at 18:52
  • Ok, it complied, but it seems that not working Commented May 1, 2017 at 19:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.