3

As you might guess from this question I am quite new to oop programming with c++. (I did only Java before) Anyways, I am trying to create a custom class for an Arduino project and get the mentioned error. Here are my files:

Header Touchable.h

#ifndef Touchable
#define Touchable
#include <Adafruit_TFTLCD.h>

#include <Arduino.h>

class Touchable {
  public:
    int posX;
    int posY;
    Touchable(int, int); //<-- Error here
    ~Touchable();
    void Touchable::draw();
};

#endif

And Touchable.cpp

#include "Touchable.h" //include the declaration for this class
#include <Adafruit_TFTLCD.h>


Touchable::Touchable(int x, int y) {
  posX = x;
  posY = y;
}

Touchable::~Touchable() {
  /*nothing to destruct*/
}

//turn the LED on
void Touchable::draw() {
  //tft.fillRect(posX, posY, 100, 100, 0x0000);
}

EDIT: Compiler message:

In file included from sketch/Touchable.cpp:1:0:
Touchable.h:11: error: expected unqualified-id before 'int'
     Touchable(int x, int y);
               ^
Touchable.h:11: error: expected ')' before 'int'
Touchable.h:12: error: expected class-name before '(' token
     ~Touchable();
               ^
Touchable.h:13: error: invalid use of '::'
     void Touchable::draw();
                          ^
Touchable.h:14: error: abstract declarator '<anonymous class>' used as declaration
 };
 ^
Touchable.cpp:5: error: expected id-expression before '(' token
 Touchable::Touchable(int x, int y) {
                     ^
Touchable.cpp:10: error: expected id-expression before '~' token
 Touchable::~Touchable() {
            ^
exit status 1
expected unqualified-id before 'int'
user7408924
  • 97
  • 2
  • 9

1 Answers1

11

You have to pick a different name for your include guard macro (commonly TOUCHABLE_H), because preprocessor translates your code in Touchable.h to:

class {
public:
   int posX;
   int posY;
   (int, int);
   ~();
   void ::draw();
};

same goes for all files that #include this one... Or you can use #pragma once.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74