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]'
2 Answers 2
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).
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.
-
Close, but where do you allocate space for
_teamIRCodes
?001– 0012017年05月01日 18:49:58 +00:00Commented 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 valuesArnyminer Z– Arnyminer Z2017年05月01日 18:52:11 +00:00Commented May 1, 2017 at 18:52
-
By now, it workedArnyminer Z– Arnyminer Z2017年05月01日 18:52:22 +00:00Commented May 1, 2017 at 18:52
-
Ok, it complied, but it seems that not workingArnyminer Z– Arnyminer Z2017年05月01日 19:02:53 +00:00Commented May 1, 2017 at 19:02