“错误:'A'不能被重载",但代码中没有任何重载 [英] "error: 'A' cannot be overloaded", but there isn't any overload in the code

查看:48
本文介绍了“错误:'A'不能被重载",但代码中没有任何重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚拥有一个 Arduino 并开始学习 C++.我决定从实现一个简单的汽车躲避"游戏开始,出现在那些 使用 LCD 和按钮的非常老的9999 合 1"手持设备.但是我遇到了一个非常奇怪的代码问题.

I've just got an Arduino and started learning C++. I decided to start by implementing a simple "car dodge" game, present in those very old "9999 in 1" handhelds using a LCD and pushbuttons. However I encountered a very strange issue with the code.

编译器认为我正在尝试覆盖 setUptearDown,但我看不出这是在哪里发生的.除了原始函数定义之外,实际上没有其他实例,但这就是我尝试编译时发生的情况(如果相关,使用 Stino 进行 Sublime Text):

The compiler believes I'm trying to override setUp and tearDown, but I can't see where this is happening. There is literally no further instances than the original function definitions, yet this is what happens when I try to compile (using Stino for Sublime Text, if that's relevant):

Compiling lcd...
Creating C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp.o...
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:31: error: 'virtual void Scene::setUp(LiquidCrystal)' cannot be overloaded
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:25: error: with 'virtual void Scene::setUp(LiquidCrystal)'
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:32: error: 'virtual void Scene::tearDown(LiquidCrystal)' cannot be overloaded
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:26: error: with 'virtual void Scene::tearDown(LiquidCrystal)'
[Stino - Error 1]

这是游戏"源文件,lcd.ino,它还没有任何游戏内容:

This is the "game" source file, lcd.ino, which has nothing of game yet:

#include "LiquidCrystal.h"

#define LEFT_BUTTON_PIN 8
#define RIGHT_BUTTON_PIN 7

#define LEFT_PRESSED B10
#define RIGHT_PRESSED B01

#define STATE_SPLASH 0
#define STATE_PLAY 1
#define STATE_SCORE 2

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);


class Scene {
    public:
    virtual void loop(byte input, LiquidCrystal lcd) {}
    virtual void setUp(LiquidCrystal lcd) {}
    virtual void tearDown(LiquidCrystal lcd) {}
};

class Splash : public Scene {
    public: void loop(byte input, LiquidCrystal lcd) {
        lcd.print("Welcome to CAR DODGE");
        lcd.print("Press LEFT to start!");
    }
};

class Game : public Scene {
    public: void loop(byte input, LiquidCrystal lcd) {
        lcd.print("HI");
    }
};

class Score : public Scene {
    public: void loop(byte input, LiquidCrystal lcd) {
        lcd.print("HI");
    }
};


byte currentMode = STATE_SPLASH;
Scene *gameModes[] = {new Splash(), new Game(), new Score()};

byte getInput() {
    bool leftSample1 = digitalRead(LEFT_BUTTON_PIN);
    bool rightSample1 = digitalRead(RIGHT_BUTTON_PIN);
    Serial.print(leftSample1);
    Serial.print(" ");
    Serial.println(rightSample1);
}

void changeGameMode(byte next) {
    gameModes[currentMode]->tearDown(lcd);
    gameModes[next]->setUp(lcd);
    currentMode = next;
}

void setup() {
    Serial.begin(9600);

    pinMode(LEFT_BUTTON_PIN, INPUT);
    pinMode(RIGHT_BUTTON_PIN, INPUT);
    lcd.begin(20, 4);

    changeGameMode(STATE_SPLASH);
}

void loop() {
    byte input = getInput();
    gameModes[currentMode]->loop(input, lcd);
    delay(150);
}

lcd.ino.cpp 内容,由 Arduino 构建工具生成:

lcd.ino.cpp contents, which is generated by the Arduino build tools:

#include <Arduino.h>
#include "LiquidCrystal.h"

#define LEFT_BUTTON_PIN 8
#define RIGHT_BUTTON_PIN 7

#define LEFT_PRESSED B10
#define RIGHT_PRESSED B01

#define STATE_SPLASH 0
#define STATE_PLAY 1
#define STATE_SCORE 2

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);


class Scene {
    public:

virtual void setUp(LiquidCrystal lcd);
virtual void tearDown(LiquidCrystal lcd);
byte getInput();
void changeGameMode(byte next);

virtual void loop(byte input, LiquidCrystal lcd) {}
    virtual void setUp(LiquidCrystal lcd) {}
    virtual void tearDown(LiquidCrystal lcd) {}
};

class Splash : public Scene {
    public: void loop(byte input, LiquidCrystal lcd) {
        lcd.print("Welcome to CAR DODGE");
        lcd.print("Press LEFT to start!");
    }
};

class Game : public Scene {
    public: void loop(byte input, LiquidCrystal lcd) {
        lcd.print("HI");
    }
};

class Score : public Scene {
    public: void loop(byte input, LiquidCrystal lcd) {
        lcd.print("HI");
    }
};


byte currentMode = STATE_SPLASH;
Scene *gameModes[] = {new Splash(), new Game(), new Score()};

byte getInput() {
    bool leftSample1 = digitalRead(LEFT_BUTTON_PIN);
    bool rightSample1 = digitalRead(RIGHT_BUTTON_PIN);
    Serial.print(leftSample1);
    Serial.print(" ");
    Serial.println(rightSample1);
}

void changeGameMode(byte next) {
    gameModes[currentMode]->tearDown(lcd);
    gameModes[next]->setUp(lcd);
    currentMode = next;
}

void setup() {
    Serial.begin(9600);

    pinMode(LEFT_BUTTON_PIN, INPUT);
    pinMode(RIGHT_BUTTON_PIN, INPUT);
    lcd.begin(20, 4);

    changeGameMode(STATE_SPLASH);
}

void loop() {
    byte input = getInput();
    gameModes[currentMode]->loop(input, lcd);
    delay(150);
}

我在这里遗漏了什么?

推荐答案

回答可能有点晚了;

Arduino IDE 似乎不喜欢在类定义中声明的成员函数.

It appears the Arduino IDE doesn't like member functions being declared in the class definition.

简单说一下:

class example {
    public:
           void function(){ /*code not here*/ };  
    private:
            int variable;
};

void example::function(){
    //code here
}  

这篇关于“错误:'A'不能被重载",但代码中没有任何重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆