C++ LinkedList of custom classes error

305 views Asked by At

I am trying to instantiate a list of shapes like this:

LinkedList<Shape> *shapes = new LinkedList<Shape>();

Using this LinkedList library. For my Arduino using Platformio to compile and upload.

I keep on getting errors when I try to compile the program. I get no flags before hand. I do not think there is anything wrong with the LinkedList library as it works fine with a struct that I use POS. The problem seems to lie with the Shape::Shape(...) constructor.

It should not be important, but, the Shape class is a simple parent class to allow mixing of specific Shape children in a single list: Circle, Ellipse, Line, etc.

// POS
struct POS {
    int x;
    int y;
};

// Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include "../stepper/POS.h"
#include "../Drive.h"

class Drive;

class Shape {
public:
    Drive *_drive;
    Shape(Drive *drive);
    ~Shape();

    POS draw();
};

#endif

//Shape.cpp
#include "Shape.h"

class Drive;

Shape::Shape(Drive *drive): _drive(drive){};

Shape::~Shape() {
    delete _drive;
};

POS Shape::draw(){
    return _drive->get();
};

Error:

[Thu Sep 14 19:51:04 2017] Processing uno (platform: atmelavr; lib_deps: LinkedList; board: uno; framework: arduino)

--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
Collected 25 compatible libraries
Looking for dependencies...
Library Dependency Graph
|-- <LinkedList>
|-- <Servo> v1.1.2
Compiling .pioenvs/uno/src/Project/main.o
In file included from src/Project/main.cpp:4:0:
.piolibdeps/LinkedList_ID443/LinkedList.h: In instantiation of 'bool LinkedList<T>::add(int, T) [with T = Shape]':
src/Project/main.cpp:50:1:   required from here
.piolibdeps/LinkedList_ID443/LinkedList.h:184:37: error: use of deleted function 'ListNode<Shape>::ListNode()'
ListNode<T> *tmp = new ListNode<T>(),
^
.piolibdeps/LinkedList_ID443/LinkedList.h:19:8: note: 'ListNode<Shape>::ListNode()' is implicitly deleted because the default definition would be ill-formed:
struct ListNode
^
.piolibdeps/LinkedList_ID443/LinkedList.h:19:8: error: no matching function for call to 'Shape::Shape()'
.piolibdeps/LinkedList_ID443/LinkedList.h:19:8: note: candidates are:
In file included from src/Project/shapes/Circle.h:3:0,
from src/Project/main.cpp:7:
src/Project/shapes/./Shape.h:11:5: note: Shape::Shape(Drive*)
Shape(Drive *drive);
^
src/Project/shapes/./Shape.h:11:5: note:   candidate expects 1 argument, 0 provided
src/Project/shapes/./Shape.h:8:7: note: constexpr Shape::Shape(const Shape&)
class Shape {
^
src/Project/shapes/./Shape.h:8:7: note:   candidate expects 1 argument, 0 provided
In file included from src/Project/main.cpp:4:0:
.piolibdeps/LinkedList_ID443/LinkedList.h: In instantiation of 'bool LinkedList<T>::add(T) [with T = Shape]':
src/Project/main.cpp:50:1:   required from here
.piolibdeps/LinkedList_ID443/LinkedList.h:199:37: error: use of deleted function 'ListNode<Shape>::ListNode()'
ListNode<T> *tmp = new ListNode<T>();
^
.piolibdeps/LinkedList_ID443/LinkedList.h: In instantiation of 'bool LinkedList<T>::unshift(T) [with T = Shape]':
src/Project/main.cpp:50:1:   required from here
.piolibdeps/LinkedList_ID443/LinkedList.h:225:37: error: use of deleted function 'ListNode<Shape>::ListNode()'
ListNode<T> *tmp = new ListNode<T>();
^
Linter
Severity    Provider    Description Line 
PIO Buildsrc/Project/shapes/Shape.cpp00011:19
LFUTF-8C++master+141 update
1

There are 1 answers

0
Drew On

Simple fix, I was being stupid. I need to have a default constructor:

in my Shape.h add Shape(); to my public functions.