C ++初学者:在' int'

时间:2017-06-15 16:53:49

标签: c++ oop arduino

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

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

和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);
}

编辑: 编译器消息:

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'

1 个答案:

答案 0 :(得分:11)

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

class {
public:
   int posX;
   int posY;
   (int, int);
   ~();
   void ::draw();
};
同样适用于#include此文件的所有文件......或者您可以使用#pragma once