1

I have a decent level of programming skill, or at least I thought so. I've been getting an error when compiling this Ardulno sketch. I don't really know what this error means or how to fix it. What's wrong with my code that's causing these errors. I was in the middle of making a big change to my code, so a lot of it is commented out because I haven't gotten to those parts.

code:

#include <LinkedList.h>

const uint8_t PinLeft = 2;
const uint8_t PinMiddle = 3;
const uint8_t PinRight = 4;
const uint8_t PinLED = 13;

enum MouseButtons {LEFT, MIDDLE, RIGHT} ;

bool leftPressed = false;
bool middlePressed = false;
bool rightPressed = false;

void sendMouseState() {
    if (leftPressed == true && middlePressed == false && rightPressed == false) {
        Mouse.set_buttons(1, 0, 0);
    } else if (leftPressed == true && middlePressed == true && rightPressed == false) {
        Mouse.set_buttons(1, 1, 0);
    } else if (leftPressed == true && middlePressed == true && rightPressed == true) {
        Mouse.set_buttons(1, 1, 1);
    } else if (leftPressed == true && middlePressed == false && rightPressed == true) {
        Mouse.set_buttons(1, 0, 1);
    } else if (leftPressed == false && middlePressed == true && rightPressed == false) {
        Mouse.set_buttons(0, 1, 0);
    } else if (leftPressed == false && middlePressed == false && rightPressed == true) {
        Mouse.set_buttons(0, 0, 1);
    } else if (leftPressed == false && middlePressed == true && rightPressed == true) {
        Mouse.set_buttons(0, 1, 1);
    } else if (leftPressed == false && middlePressed == false && rightPressed == false) {
        Mouse.set_buttons(0, 0, 0);
    }
}

void mouseButtonPress(MouseButtons theButton);
void mouseButtonPress(MouseButtons theButton) {
    if (theButton == MouseButtons::LEFT) {
        leftPressed = true;
    } else if(theButton == MouseButtons::MIDDLE) {
        middlePressed = true;
    } else if (theButton == MouseButtons::RIGHT) {
        rightPressed = true;
    }
}

void mouseButtonRelease(MouseButtons button);
void mouseButtonRelease(MouseButtons button) {
    if (button == LEFT) {
        leftPressed = false;
    } else if(button == MIDDLE) {
        middlePressed = false;
    } else if (button == RIGHT) {
        rightPressed = false;
    }
}

volatile unsigned int nextPressLeft = 0;
volatile unsigned int nextPressCenter = 0;
volatile unsigned int nextPressRight = 0;
const int timeBetween = 150; //milliseconds

enum ActionType {KB, MOU}; //keyboard or mouse

class Action {
    public:
        uint16_t keyCode;
        ActionType actionType;

        Action (uint16_t keyCode, ActionType actionType) {
            this->keyCode = keyCode;
            this->actionType = actionType;
        }

};

class ModeAction {

    public:
        LinkedList<Action> *keys = new LinkedList<Action>();

        void addAction(Action action) {
            keys->add(action);
        }

};

class ActionManager {

    private:
        uint8_t modeL = 1;
        uint8_t modeM = 1;
        uint8_t modeR = 1;

        LinkedList<ModeAction> *lActions = new LinkedList<ModeAction>();
        LinkedList<ModeAction> *mActions=new LinkedList<ModeAction>();
        LinkedList<ModeAction> *rActions=new LinkedList<ModeAction>();

        bool lActing = false, mActing = false, rActing = false;

    public:
        ActionManager() {
            //actions for left switch
            //mode 1
            lActions->add(ModeAction());
            lActions->get(lActions->size() - 1).addAction(Action(MouseButtons.LEFT, ActionType.MOU));
            //mode 2
            lActions->add(ModeAction());
            lActions->get(lActions->size() - 1).addAction(Action(KEY_F11, ActionType.KB));
            //mode 3
            lActions->add(ModeAction());
            lActions->get(lActions->size() - 1).addAction(Action(KEY_LEFT_CTRL, ActionType.KB));
            lActions->get(lActions->size() - 1).addAction(Action(KEY_LEFT_ALT, ActionType.KB));
            //mode 4
            lActions->add(ModeAction());
            lActions->get(lActions->size() - 1).addAction(Action(KEY_W, ActionType.KB));

            //actions for middle switch
            //mode 1
            mActions->add(ModeAction());
            mActions->get(lActions->size() - 1).addAction(Action(MouseButtons.MIDDLE, ActionType.MOU));

            //actions for right switch
            //mode 1
            rActions->add(ModeAction());
            rActions->get(lActions->size() - 1).addAction(Action(MouseButtons.RIGHT, ActionType.MOU));

        }


        void cycleModeL() {
            this->modeL++;
            if(this->modeL >= lActions->size()) {
                this->modeL = 1;
            }
        }

        void cycleModeM() {
            this->modeM++;
            if(this->modeM >= mActions->size()) {
                this->modeM = 1;
            }
        }

        void cycleModeR() {
            this->modeR++;
            if(this->modeR >= rActions->size()) {
                this->modeR = 1;
            }
        }

    private:

        //void doDownActions(ModeAction modeActions, uint8_t mode) {
            //for (int i = 0; i < modeActions.at(mode).keys.length(); i++) {
                //if (modeActions[mode][i].keys[i].actionType == ActionType.KB) {
                    //Keyboard.press(modeActions[mode].keys[i].keyCode)
                //} else if (modeActions[mode].keys[i].actionType == ActionType.MOU) {
                    //mouseButtonPress(modeActions[mode].keys[i].keyCode);
                //}
            //}
            //sendMouseState();
        //}
//
        //void doUpActions(ModeAction modeActions, uint8_t mode) {
            //for (int i = 0; i < modeActions[mode].keys.length(); i++) {
                //if (modeActions[mode][i].keys[i].actionType == ActionType.KB) {
                    //Keyboard.release(modeActions[mode].keys[i].keyCode)
                //} else if (modeActions[mode].keys[i].actionType == ActionType.MOU) {
                    //mouseButtonRelease(modeActions[mode].keys[i].keyCode);
                //}
            //}
            //sendMouseState();
        //}

    public:

        //void leftDown() {
            //if (lActing == false) {
                //lActing = true;
                //doDownActions(lActions, modeL);
            //}
        //}
//
        //void middleDown() {
            //if (mActing == false) {
                //mActing = true;
                //doDownActions(mActions, modeM);
            //}
        //}
//
        //void rightDown() {
            //if (rActing == false) {
                //rActing = true;
                //doDownActions(rActions, modeR);
            //}
        //}
//
        //void leftUp() {
            //if (lActing == true) {
                //lActing = false;
                //doUpActions(lActions, modeL);
            //}
        //}
//
        //void middleUp() {
            //if (mActing == true) {
                //mActing = false;
                //doUpActions(mActions, modeM);
            //}
        //}
//
        //void rightUp() {
            //if (rActing == true) {
                //rActing = false;
                //doUpActions(rActions, modeR);
            //}
        //}

};

ActionManager actionManager = ActionManager();

void setup() {

    Serial.begin(115200);

    pinMode(PinLED, OUTPUT);

    //pinMode(PinRight, INPUT_PULLUP); // sets the digital pin as output
    //attachInterrupt(PinRight, isrChangeR, CHANGE); // interrrupt is data ready
//
    //pinMode(PinMiddle, INPUT_PULLUP); // sets the digital pin as output
    //attachInterrupt(PinMiddle, isrChangeM, CHANGE); // interrrupt is data ready
//
    //pinMode(PinLeft, INPUT_PULLUP); // sets the digital pin as output
    //attachInterrupt(PinLeft, isrChangeL, CHANGE); // interrrupt is data ready

}

void loop() {

    //if (Serial.available()) {
////change mode
        //mode = Serial.parseInt();
//
////release all buttons
        //Keyboard.releaseAll();
        //Mouse.set_buttons(0, 0, 0);
//
////turn off LED
        //digitalWrite(PinLED, LOW);
//
////flush the serial input buffer
        //while (Serial.available()) {
            //Serial.read();
        //}
//
    //}
    //delay(1000);
    //Serial.println("Running");
}

//void isrChangeL() {
    //cli();
    //if (digitalRead(PinLeft) == LOW && nextPressLeft < millis()) {
////Serial.println("Pressed");
        //digitalWrite(PinLED, HIGH); //set the LED on
//
        //if (mode == 0) {
            //Mouse.set_buttons(1, 0, 0);
        //} else if (mode == 1) {
            //Keyboard.press(KEY_F11);
        //} else if (mode == 2) {
            //Keyboard.press(KEY_LEFT_CTRL);
            //Keyboard.press(KEY_LEFT_ALT);
        //} else if (mode == 3) {
            //Keyboard.press(KEY_W);
        //}
//
        //nextPressLeft = millis() + timeBetween;
//
    //} else {
//// Serial.println("Released");
        //digitalWrite(PinLED, LOW); // set the LED off
//
        //if (mode == 0) {
            //Mouse.set_buttons(0, 0, 0);
        //} else if (mode == 1) {
            //Keyboard.release(KEY_F11);
        //} else if (mode == 2) {
            //Keyboard.release(KEY_LEFT_CTRL);
            //Keyboard.release(KEY_LEFT_ALT);
        //} else if (mode == 3) {
            //Keyboard.release(KEY_W);
        //}
//
        //nextPressLeft = millis() + timeBetween * 0.6;
//
//
    //}
    //sei();
//}
//
//void isrChangeM() {
    //cli();
////if (digitalRead(PinMiddle) == LOW && nextPressCenter < millis())
////{
////Keyboard.press(KEY_SPACE);
////delay(250);
////Keyboard.release(KEY_SPACE);
////}
//
    //if (digitalRead(PinMiddle) == LOW && nextPressCenter < millis()) {
////Serial.println("Pressed");
        //digitalWrite(PinLED, HIGH); //set the LED on
////Mouse.set_buttons(0, 1, 0);
//
        //nextPressCenter = millis() + timeBetween;
//
    //} else {
//// Serial.println("Released");
        //digitalWrite(PinLED, LOW); // set the LED off
////Mouse.set_buttons(0, 0, 0);
//
        //nextPressCenter = millis() + timeBetween * 0.6;
//
//
    //}
//
    //sei();
//}
//
//void isrChangeR() {
    //cli();
    //if (digitalRead(PinRight) == LOW && nextPressRight < millis()) {
////Serial.println("Pressed");
        //digitalWrite(PinLED, HIGH); // set the LED on
        //Mouse.set_buttons(0, 0, 1);
////Keyboard.press(KEY_F11);
        //nextPressRight = millis() + timeBetween;
    //} else {
//// Serial.println("Released");
        //digitalWrite(PinLED, LOW); // set the LED off
        //Mouse.set_buttons(0, 0, 0);
////Keyboard.release(KEY_F11);
        //nextPressRight = millis() + timeBetween * 0.6;
    //}
    //sei();
//}

compiler log:

Compiling debug version of 'click_switch_5_cycling' for 'Teensy LC'
click_switch_5_cycling.ino: In constructor ActionManager::ActionManager()

click_switch_5_cycling.ino: 106:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 106:66: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 106:83: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 109:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 109:73: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 112:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 112:79: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 113:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 113:78: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 116:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 116:71: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 121:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 121:66: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 121:85: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 126:53: error: expected primary-expression before '(' token

click_switch_5_cycling.ino: 126:66: error: expected primary-expression before '.' token

click_switch_5_cycling.ino: 126:84: error: expected primary-expression before '.' token

click_switch_5_cycling.ino:1: In file included from
LinkedList.h: In instantiation of bool LinkedList<T>::add(T) [with T = Action]
click_switch_5_cycling.ino:83: required from here

LinkedList.h: 192:37: error: use of deleted function 'ListNode<Action>::ListNode()
ListNode<T> *tmp = new ListNode<T>()
LinkedList.h:19: note  ListNode<Action>  ListNode() is implicitly deleted because the default definition would be ill-formed
struct ListNode

LinkedList.h: 19:8: error: no matching function for call to 'Action::Action()
click_switch_5_cycling.ino:70: note  candidate  Action  Action(uint16_t, ActionType)
this->actionType = actionType
click_switch_5_cycling.ino:70: note    candidate expects 2 arguments, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(const Action&)
uint16_t keyCode
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(Action&&)
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided

click_switch_5_cycling.ino:1: In file included from
LinkedList.h: In instantiation of ListNode<T>* LinkedList<T>::getNode(int) [with T = ModeAction]
LinkedList.h:314: required from T LinkedList<T>  get(int) [with T = ModeAction]
click_switch_5_cycling.ino:106: required from here
LinkedList.h: 160:9: warning: converting 'false' to pointer type 'ListNode<ModeAction>*' [-Wconversion-null]
return false
LinkedList.h: In instantiation of bool LinkedList<T>::add(int, T) [with T = Action]
click_switch_5_cycling.ino:262: required from here

LinkedList.h: 177:37: error: use of deleted function 'ListNode<Action>::ListNode()
ListNode<T> *tmp = new ListNode<T>()
LinkedList.h: In instantiation of bool LinkedList<T>::unshift(T) [with T = Action]
click_switch_5_cycling.ino:262: required from here

LinkedList.h: 218:37: error: use of deleted function 'ListNode<Action>::ListNode()
ListNode<T> *tmp = new ListNode<T>()
LinkedList.h: In instantiation of T LinkedList<T>::remove(int) [with T = Action]
click_switch_5_cycling.ino:262: required from here

LinkedList.h: 290:12: error: no matching function for call to 'Action::Action()
return T()
click_switch_5_cycling.ino:70: note  candidate  Action  Action(uint16_t, ActionType)
this->actionType = actionType
click_switch_5_cycling.ino:70: note    candidate expects 2 arguments, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(const Action&)
uint16_t keyCode
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(Action&&)
Error compiling project sources
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided
Debug build failed for project 'click_switch_5_cycling'

click_switch_5_cycling.ino:1: In file included from
LinkedList.h: In instantiation of T LinkedList<T>::pop() [with T = Action]
click_switch_5_cycling.ino:262: required from here

LinkedList.h: 242:12: error: no matching function for call to 'Action::Action()
return T()
click_switch_5_cycling.ino:70: note  candidate  Action  Action(uint16_t, ActionType)
this->actionType = actionType
click_switch_5_cycling.ino:70: note    candidate expects 2 arguments, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(const Action&)
uint16_t keyCode
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(Action&&)
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided

click_switch_5_cycling.ino:1: In file included from
LinkedList.h: In instantiation of T LinkedList<T>::shift() [with T = Action]
click_switch_5_cycling.ino:262: required from here

LinkedList.h: 268:12: error: no matching function for call to 'Action::Action()
return T()
click_switch_5_cycling.ino:70: note  candidate  Action  Action(uint16_t, ActionType)
this->actionType = actionType
click_switch_5_cycling.ino:70: note    candidate expects 2 arguments, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(const Action&)
uint16_t keyCode
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(Action&&)
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided

click_switch_5_cycling.ino:1: In file included from
LinkedList.h: In instantiation of T LinkedList<T>::get(int) [with T = Action]
click_switch_5_cycling.ino:262: required from here

LinkedList.h: 316:14: error: no matching function for call to 'Action::Action()
T())
click_switch_5_cycling.ino:70: note  candidate  Action  Action(uint16_t, ActionType)
this->actionType = actionType
click_switch_5_cycling.ino:70: note    candidate expects 2 arguments, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(const Action&)
uint16_t keyCode
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided
click_switch_5_cycling.ino:65: note  candidate  constexpr Action  Action(Action&&)
click_switch_5_cycling.ino:65: note    candidate expects 1 argument, 0 provided

Thanks for any help you can give me about what these errors are and how to fix them.

0

3 Answers 3

3

You seem to have been programming in Java recently, because many of your errors have a Java ‘whiff’ to them. This, as you’ve tagged, is C++, not Java.

  1. sendMouseState:

    a. You wrote leftPressed == true. This is much more simply expressed by the expression leftPressed.

    b. You wrote middlePressed == false. This is much more simply expressed using the “not” operator !, as in !middlePressed.

    c. The code tests all eight possible combinations of leftPressed, middlePressed and rightPressed, calling Mouse.set_buttons(#,#,#);

    However, bools evaluate to numbers: false evaluates to 0, while true evaluates to 1. All eight tests, and therefore the entire function, can therefore be boiled down to the single line:

    Mouse.set_buttons(leftPressed, middlePressed, rightPressed);

  2. You declared to the compiler, in a few places, “There’s going to be a function called (e.g) void mouseButtonPress(MouseButtons theButton);

    That’s fair enough - but the compiler only needs that if you’re going to call the function before you define it. Since you follow the declarations immediately by the definitions, the declarations are unnecessary: get rid of them.

  3. Use of enums:

    Declaring an enum lets you use symbols instead of raw numbers: well done! However, that’s all you need to use: the enum’s symbol: LEFT, MIDDLE or RIGHT is all you need.

    If you do feel the need to express the enum’s name, you need to use the :: operator, not the . operator: MouseButtons::LEFT.

  4. Use of new:

    You do not need to call new to create members of classes. Well, OK, you might if they’re declared as pointers (which you have done), but that’s your problem: you should declare them as simple members, not pointer members.

    For example, instead of:

    LinkedList<Action> *keys = new LinkedList<Action>();
    

    you simply need:

    LinkedList<Action> keys;
    

    Note that no new is needed now. Then, everywhere that you dereference the member with keys->add(action);, you use keys.add(action); instead.

  5. Use of this->:

    You use this-> a lot. You practically never have to: however, the one time you did need to was in the code:

    Action (uint16_t keyCode, ActionType actionType) {
        this->keyCode = keyCode;
        this->actionType = actionType;
    }
    

    Here, this-> was needed to distinguish the class members from the passed-in parameters that have the same name. However, if you’d written the constructor correctly, using initialisation instead of assignment, you wouldn’t have needed this-> here either:

    Action (uint16_t keyCode, ActionType actionType) :
        keyCode(keyCode),
        actionType(actionType) {
    }
    
  6. add() followed by get():

    You use the following code:

    lActions.add(ModeAction());
    lActions.get(lActions->size() - 1).addAction(Action(LEFT,MOU));
    

    That is, you add an empty ModeAction, then fetch it back with get(...) before manipulating it. Don’t.

    Instead, create a temporary ModeAction object, get it ready, then add() it to the list:

    ModeAction lMode1;
    lMode1.addAction(Action(LEFT,MOU));
    lActions.add(lMode1);
    
5
  • 1a and 1b are bad practice in Java too, 1c and 6 are bad algorithm (n Java too)
    – Juraj
    Jul 29, 2018 at 15:00
  • Dart, actually - been trying to learn it. Thanks for your detailed response. 1. a,b. I'm working with other people who aren't really programmers so I wanted to keep it explicit. 1. c. That's a good point, thanks. 2. I did that because I was getting the error: "variable or field 'mouseButtonPress' declared void" and I read that that was how to fix it. Something about the way Arduino handles function definitions. 3. I want the code to be explicit, so I'll change those to :: .
    – dude8604
    Jul 30, 2018 at 1:54
  • 4. I was using example code given by the LinkedList library (github.com/ivanseidel/LinkedList). 5. Ok. 6. Ok, I changed those. I think I made all the changes you suggested, but it still won't compile. Here's the new code gist.github.com/dude8604/98f7d8e7e6f4d74349b1b974e8ea0d17 and compiler log gist.github.com/dude8604/e9685c4b49b6c90922ad09a69b5e4c34 It seems like most of the errors point to the LinkedList library. Are the remaining errors with the library or my code? Thanks.
    – dude8604
    Jul 30, 2018 at 1:54
  • @dude8604 4. That example LinkedList code is... abbreviated. It starts simply enough with a normal variable, then talks about creating one with new. It wasn't implying that was how it had to be done... It's also got a bug in it: I'll send a Pull request to the author. Edit: No need - it's already there, just not actioned. Sigh! Aug 1, 2018 at 4:01
  • @dude8604 Since you want to create a linked list of Actions, in your Action class you need to have a "default" constructor: one which takes zero parameters. If you add the line "Action () : keyCode(0), actionType(KB) {}" to the class, LinkedList<Action> will now compile. Aug 1, 2018 at 4:01
1

The compiler is giving you a clue that the problem is around line 106. You have there:

  LinkedList<ModeAction> *lActions = new LinkedList<ModeAction>();
  LinkedList<ModeAction> *mActions=new LinkedList<ModeAction>();
  LinkedList<ModeAction> *rActions=new LinkedList<ModeAction>();

You can't do procedural code (like new) in a data declaration. Also, I doubt that there is a function to be called so I would get rid of the brackets. Initializing member fields should be done in the constructor, not in the declaration.

4
  • I moved the assignment part into the constructor but I'm still getting similar errors. Should I edit my code and compiler log in the original post?
    – dude8604
    Jul 29, 2018 at 6:57
  • should not be a problem with C++11 en.cppreference.com/w/cpp/language/data_members
    – Juraj
    Jul 29, 2018 at 14:09
  • This isn't a forum, so to-ing and fro-ing with changes to the original post makes the question a moving target, and then the answers look meaningless. Hopefully John Burger's rather comprehensive answer will help you without you having to do that.
    – Nick Gammon
    Jul 29, 2018 at 22:09
  • Juraj is right however that declaring pointers like that is OK under C++11. However as John Burger points out, it is easier not to use pointers in the first place. For one thing, if you are going to use new then you should use delete in the destructor.
    – Nick Gammon
    Jul 29, 2018 at 22:13
0

you use wrong syntax to access an enum member. use :: not .

Action(MouseButtons::LEFT, ActionType::MOU)

EDIT:

second error is that LinkedList requires constructor without parameters on the template type

add constructor without parameters to class Action

3
  • Re: second error, Is there another Arduino vector library that wouldn't require that?
    – dude8604
    Jul 31, 2018 at 3:04
  • I tried it and now the program freezes.
    – dude8604
    Jul 31, 2018 at 3:42
  • why do you want to use dynamic vector? use array
    – Juraj
    Jul 31, 2018 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.