C++初学者:'int'之前的预期unqualified-id

use*_*924 3 c++ oop arduino

正如你可能从这个问题中猜到的那样,我对使用c ++的oop编程很新.(之前我只做过Java)无论如何,我正在尝试为Arduino项目创建一个自定义类并得到上面提到的错误.这是我的文件:

标头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
Run Code Online (Sandbox Code Playgroud)

和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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:编译器消息:

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'
Run Code Online (Sandbox Code Playgroud)

Log*_*uff 11

您必须为包含保护宏(通常TOUCHABLE_H)选择一个不同的名称,因为预处理器会将您的代码转换Touchable.h为:

class {
public:
   int posX;
   int posY;
   (int, int);
   ~();
   void ::draw();
};
Run Code Online (Sandbox Code Playgroud)

同样适用于#include这一个的所有文件......或者你可以使用#pragma once.