Hi all, I'm trying to write a linked list library to allow my sketch to create dynamically sized lists. A linked list seemed to be an obvious way to do that but I cannot get it to compile.
Here are my files:
LinkedList.h
#ifndef _LinkedList_H_
#define _LinkedList_H_
#include "Arduino.h"
class Node {
public:
Node(double value);
void setValue(double value);
void setNext(Node next);
double getValue();
Node getNext();
private:
double value;
Node* nextNode;
};
class LinkedList {
public:
LinkedList(double& value);
void append(double value);
double get(int index);
void remove(int index);
int size();
private:
Node node1;
Node lastNode;
int length = 0;
};
//Do not add code below this line
#endif /* _LinkedList_H_ */
LinkedList.cpp
// Do not remove the include below
#include "LinkedList.h"
LinkedList :: LinkedList(double& value) {
node1(value);
lastNode = node1;
length = 1;
}
void LinkedList :: append(double value) {
Node node(value);
lastNode.setNext(node);
lastNode = node;
length++;
}
double LinkedList :: get(int index) {
int x = 0;
Node* nextNode = &node1;
while(nextNode != NULL) {
if(x == index) {
return (*nextNode).getValue();
}
x++;
nextNode = &(*nextNode).getNext();
}
return NULL; // Shouldn't ever reach this statement
}
void LinkedList :: remove(int index) {
if (index < 0 || index >= length) {
return;
}
if(index == 0) {
node1 = node1.getNext();
} else {
int x = 1;
Node* thisNode = &node1.getNext();
Node lastNode = node1;
while(thisNode != NULL) {
if(x == index) {
lastNode.setNext(thisNode.getNext());
}
lastNode = lastNode.getNext();
x++;
thisNode = &(*thisNode).getNext();
}
}
length--;
}
int LinkedList :: size() {
return length;
}
/**************************************************************
* Node class
* ************************************************************
*/
Node :: Node(double value) {
this->value = value;
nextNode = NULL;
}
void Node :: setValue(double value) {
this->value = value;
}
void Node :: setNext(Node next) {
nextNode = &next;
}
double Node :: getValue() {
return this->value;
}
Node Node :: getNext() {
return *nextNode;
}
I get several errors on compilation:
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp: In constructor 'LinkedList::LinkedList(double)':
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:5:36: error: no matching function for call to 'Node::Node()'
LinkedList::LinkedList(double value){
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:5:36: note: candidates are:
In file included from C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:2:0:
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:14:2: note: Node::Node(double)
Node(double value);
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:14:2: note: candidate expects 1 argument, 0 provided
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: constexpr Node::Node(const Node&)
class Node {
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: candidate expects 1 argument, 0 provided
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: constexpr Node::Node(Node&&)
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: candidate expects 1 argument, 0 provided
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:5:36: error: no matching function for call to 'Node::Node()'
LinkedList::LinkedList(double value){
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:5:36: note: candidates are:
In file included from C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:2:0:
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:14:2: note: Node::Node(double)
Node(double value);
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:14:2: note: candidate expects 1 argument, 0 provided
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: constexpr Node::Node(const Node&)
class Node {
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: candidate expects 1 argument, 0 provided
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: constexpr Node::Node(Node&&)
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: candidate expects 1 argument, 0 provided
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:6:8: error: no match for 'operator=' (operand types are 'Node' and 'Node*')
node1 = new Node(value);
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:6:8: note: candidates are:
In file included from C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:2:0:
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: Node& Node::operator=(const Node&)
class Node {
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: no known conversion for argument 1 from 'Node*' to 'const Node&'
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: Node& Node::operator=(Node&&)
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.h:12:7: note: no known conversion for argument 1 from 'Node*' to 'Node&&'
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp: In member function 'void LinkedList::append(double)':
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:12:28: error: conversion from 'Node*' to non-scalar type 'Node' requested
Node node = new Node(value);
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp: In member function 'double LinkedList::get(int)':
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:28:19: error: no match for 'operator!=' (operand types are 'Node' and 'int')
} while(nextNode != NULL);
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp: In member function 'void LinkedList::remove(int)':
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:50:20: error: no match for 'operator!=' (operand types are 'Node' and 'int')
} while(thisNode != NULL);
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp: In member function 'void Node::setNext(Node)':
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:74:17: error: cannot convert 'Node' to 'Node*' in assignment
this->nextNode = next;
^
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp: In member function 'Node Node::getNext()':
C:\Program Files (x86)\Arduino\libraries\LinkedList\LinkedList.cpp:82:15: error: could not convert '((Node*)this)->Node::nextNode' from 'Node*' to 'Node'
return this->nextNode;
^
exit status 1
Error compiling.
For context: I'm usually a Java/Python developer and I'm using eclipse & Sloeber plugin.
I think my problems might have something to do with pointers but my attempts to include */& in this were only marginally fruitful.
Can anybody spot any obvious mistakes I have made? Am I just being a noob?
Thanks