Parsing array in library constructor - Pointer problem (C++)

大憨熊 提交于 2019-12-13 04:12:58

问题


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.

来源:https://stackoverflow.com/questions/55375221/parsing-array-in-library-constructor-pointer-problem-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!