Hello everyone! I am trying to dynamically allocate an array located in a class using a method I defined. Unfortunately I got stuck in the process.
I tried to simplyfy the code to clearly show my problem. In the class I allocate the array using malloc in the constructor and in the method add_element I fill it. The method add_element get as parameters a string and the position where to put it in the array.
class myClass
{
private:
int arr_size;
public:
const String * myarr;
myClass(int _arr_size) //Constructor
{
arr_size = _arr_size;
myarr = (const String *)malloc(arr_size); //allocating the array
}
void add_element(const String *element, int pos)
{
myarr[pos] = element;
}
};
const String arr[2] = {"Obj1", "Obj2"};
myClass myclass(2); //class definition
void setup()
{
//filling the array
myclass.add_element(&arr[0], 0);
myclass.add_element(&arr[1], 1);
}
void loop() {}
I know that it makes little sense to fill an array like that but it is part of project I am working on.
The error i get is the following:
[..]sketch_mar24a.ino: In member function 'void myClass::add_element(const String*, int)':
[..]sketch_mar24a.ino:16:16: warning: passing 'const String' as 'this' argument of 'String& String::operator=(StringSumHelper&&)' discards qualifiers [-fpermissive]
myarr[pos] = element;
^
sketch_mar24a:16: error: conversion from 'const String*' to 'StringSumHelper' is ambiguous
[..]sketch_mar24a.ino:14:34: note: candidates are:
void add_element(const String *element, int pos)
^
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:231:0,
from sketch\sketch_mar24a.ino.cpp:1:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:223:2: note: StringSumHelper::StringSumHelper(long unsigned int) <near match>
StringSumHelper(unsigned long num) : String(num) {}
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:223:2: note: no known conversion for argument 1 from 'const String*' to 'long unsigned int'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:222:2: note: StringSumHelper::StringSumHelper(long int) <near match>
StringSumHelper(long num) : String(num) {}
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:222:2: note: no known conversion for argument 1 from 'const String*' to 'long int'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:221:2: note: StringSumHelper::StringSumHelper(unsigned int) <near match>
StringSumHelper(unsigned int num) : String(num) {}
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:221:2: note: no known conversion for argument 1 from 'const String*' to 'unsigned int'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:220:2: note: StringSumHelper::StringSumHelper(int) <near match>
StringSumHelper(int num) : String(num) {}
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:220:2: note: no known conversion for argument 1 from 'const String*' to 'int'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:219:2: note: StringSumHelper::StringSumHelper(unsigned char) <near match>
StringSumHelper(unsigned char num) : String(num) {}
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:219:2: note: no known conversion for argument 1 from 'const String*' to 'unsigned char'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:218:2: note: StringSumHelper::StringSumHelper(char) <near match>
StringSumHelper(char c) : String(c) {}
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:218:2: note: no known conversion for argument 1 from 'const String*' to 'char'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:91:11: note: initializing argument 1 of 'String& String::operator=(StringSumHelper&&)'
String & operator = (StringSumHelper &&rval);
^
exit status 1
conversion from 'const String*' to 'StringSumHelper' is ambiguous
Thank you for your help!