加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:14:08 所属栏目:百科 来源:网络整理
导读:正如你可能从这个问题中猜到的那样,我对使用c编程的oop很新. (之前我只做过 Java) 无论如何,我正在尝试为Arduino项目创建一个自定义类并获得上述错误.这是我的文件: 标头Touchable.h #ifndef Touchable#define Touchable#include Adafruit_TFTLCD.h#include
正如你可能从这个问题中猜到的那样,我对使用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

和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,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'

解决方法

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

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

同样适用于#include this one的所有文件……或者你可以使用#pragma once.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读