0

I am trying to develop an Arduino library that consists out of two classes. I want 'WayPointStack' to store an array of 'WPCommand', but I can't get it to work.

It is obviously a pointer problem, but I don't know how to solve it. There are four errors left in my code:

WayPointStack.cpp:23:7: error: incompatible types in assignment of 'WPCommand*' to 'WPCommand [0]'
_wp = new WPCommand[arrSize]; //Fixed

WayPointStack.cpp:44:34: error: could not convert '(operator new(16u), (((WPCommand*)<anonymous>)->WPCommand::WPCommand(4, 10000), ((WPCommand*)<anonymous>)))' from 'WPCommand*' to 'WPCommand'
return new WPCommand(_END, 10000);

WayPointStack.cpp:59:15: error: no match for 'operator=' (operand types are 'WPCommand' and 'WPCommand*')
_wp[pointer] = new WPCommand(target, time); # Fixed

WPCommand.h:10:7: note: candidate: WPCommand& WPCommand::operator=(const WPCommand&)
class WPCommand # Does not appear anymore, fixed

WPCommand.h

#ifndef WPCommand_h
#define WPCommand_h

#include "Arduino.h"

class WPCommand
{
  public:
    WPCommand(int target, int time );
    WPCommand();
    int GetTarget();
    int GetTime();
    int LEFT;
    int RIGHT;
    int FORWARD;
    int BACKWARD;
    int STOP;
    int END;
  private:
    int _target;
    int _time;
};
#endif

WayPointStack.h

#ifndef WayPointStack_h
#define WayPointStack_h

#include "Arduino.h"
#include "WPCommand.h"

class WayPointStack
{
  public:
    WayPointStack();
    WayPointStack(WPCommand wp[], int length);
    WPCommand GetNextWP();
    WPCommand GetWP(int i);
    void AddWP(int target, int time);
    int SetWPPointer(int i);
    int GetWPPointer();
    int GetLength();
  private:
    WPCommand _wp[];
    int pointer;
    int _length;
};

#endif

WayPointStack.cpp (partly)

#include "Arduino.h"
#include "WayPointStack.h"
#include "WPCommand.h"
#define _BACKWARD 0
#define _FORWARD 1
#define _STOP 2
#define _LEFT 3
#define _RIGHT 4
#define _END 4
#define arrSize 100

WayPointStack::WayPointStack()
{
  _wp = new WPCommand[arrSize];
  _length = 0;
  pointer = 0;
}

WayPointStack::WayPointStack(WPCommand wp[], int length)
{
    _wp = new WPCommand[arrSize];
    for (int i = 0; i < length; i++){
        _wp[i] = wp[i];
    }
    _length = length;
    pointer = 0;
}

WPCommand WayPointStack::GetNextWP()
{
    if (pointer < _length){
        pointer++;
        return _wp[pointer-1];
    }
    return new WPCommand(_END, 10000);
}

I tried to more or less randomly reference and derefence _wp and wp[] but it did not work.

Edit: Changing

WPCommand _wp[];

to

WPCommand *_wp;

successfully fixed the first error. Doing the same to

WayPointStack(WPCommand *wp, int length);

Edit:

_wp[pointer] = WPCommand(target, time);

instead of

_wp[pointer] = new WPCommand(target, time);

successfully fixed error number 3.

Edit:

return WPCommand(_END, 10000);

fixed error number 2.

Problem solved.

Kruspe
  • 626
  • 6
  • 19
  • 1
    `WPCommand _wp[];` is not a valid definition. You need to make it a pointer: `WPCommand *_wp;` – Some programmer dude Mar 27 '19 at 10:43
  • Does your code compile when you declare a zero sized array `WPCommand _wp[];` in your class `WayPointStack`? – vahancho Mar 27 '19 at 10:48
  • Thank you. I got rid of the first error. But declaring a zero sized array had no effect at all. I updated the question – Kruspe Mar 27 '19 at 11:01
  • 1
    Are you sure that you do not want to make your life simple by using a container class like `std::vector`? (Likewise, those `#define _BACKWARD 0` look like you want to use an `enum` instead.) – Aziuth Mar 27 '19 at 11:01
  • Second error might be fixed by using braces, `return (new WPCommand(_END, 10000));`. Kind of strange to return something that does have nothing to do with the object, though. – Aziuth Mar 27 '19 at 11:02
  • 1
    In the third error, you get the element of an array, which is of non-pointer type, and you assign a pointer to it. As the error does not happen in the code you posted, no idea what you try there, but should be clear why this can't work. – Aziuth Mar 27 '19 at 11:09
  • Unfortunately it doesn't fix the problem (error 2). The whole system is about letting a small robot move long a path with given speed. (move forward for one second, then rotate left for 0.25 seconds..) If there are no commands left, send an 'end command'.Since the iterator function requires a WPCommand object, I just chose this way. And yes, you are correct. I should use an enum instead. It's on my list. =) – Kruspe Mar 27 '19 at 11:12
  • I got rid of the third error by referencing it. I think this should do the job. Thank you very much. But error number two is this there. The braces did not fix it. – Kruspe Mar 27 '19 at 11:19
  • @Kruspe you introduced new problems with your changes :-( ( https://stackoverflow.com/questions/55378196/creating-a-second-instance-of-an-object-changes-whole-class-behavior-c ) – bruno Mar 27 '19 at 13:28
  • Is that a bad thing? I thought it's a new question and so the right thing to do :/ – Kruspe Mar 27 '19 at 13:31
  • 1
    Next time, please answer with an @user, if you don't, I don't get a notification that you answered to my comment (you get notifications about my comments because it's your question). Now, I took another look at error two: your function is of non-pointer return type, yet you try to return a pointer. Again: your function has the signature `WPCommand WayPointStack::GetNextWP()`, not `WPCommand* WayPointStack::GetNextWP()`. Either change the signature or return a non-pointer (`return WPCommand(_END, 10000);`, without the `new`) – Aziuth Mar 27 '19 at 15:01

0 Answers0