0

I boiled some code of my Arduino Project down to this:

#include <iostream>
class myClass {

public:
  int foo;
  myClass() {foo = 3;};
};

myClass * f_dummy() {
  myClass * ptr_ret;

  ptr_ret = new myClass;

  return ptr_ret;
};

int main() {
  myClass * ptr_class;
  ptr_class = f_dummy();
  std::cout << ptr_class->foo;

  delete ptr_class;
}

Basically, i want to create a new instance of myClass in f_dummy() and return its Pointer. http://cpp.sh seems not to have problems with compiling and running this. (I get "3" on the console) The Arduino IDE on the other hand gives me a compiler error: error: expected constructor, destructor, or type conversion before '*' token

I wonder why these two compilers behave different in this case and if there is a way to get my code to work in the arduino IDE. (I'm new to C++)

  • 4
    On which exactly line/position do you get the error? Please post the whole error text. – Anton Savin Dec 09 '14 at 21:59
  • 3
    Does the code in your post exactly reflect what you have in the Arduino IDE? The error you quote is the sort of thing I'd expect to see if you had `myClass * f_dummy() { ... }` above `class myClass { ... };`, for example. – dlf Dec 09 '14 at 22:01
  • Just a style remark, your f_dummy method can directly return the pointer without declaring intermediate variable that is : myClass * f_dummy() { return new MyClass;} – geoalgo Dec 09 '14 at 22:04
  • This is exactly the code I have in my IDE. It seems the error always ocurs on the line right after "class myClass {". (in this case line 3) – fdiwald Dec 10 '14 at 20:41
  • I must confess I modified the code a bit to run on cpp.sh. For a real world Arduino project you would replace the cout by the Serial-Interface and int main() by void setup() but the error is the same. – fdiwald Dec 10 '14 at 20:55
  • I have a suggestion which may not solve the problem but I can share what I used to do while programming Arduino using library headers. Please make separate files for classes and globals, then include them into your main sketch file. You can check then if the sources compiles without error. – Tajuddin Khandaker Dec 08 '19 at 11:27

0 Answers0