我一直在用Tinkercad的模拟器在Arduino Uno上写一个简单的游戏。我创建了一个名为Bullet的类和一个名为MainCharacter的类,我将它直接放到了Tinkercad的代码编辑器中,因为它不能像普通的Arduino编译器那样导入多个.h和.cpp文件。
class Bullet{
private:
int xLocation;
int yLocation;
int flySpeed;
bool isBetween;
public:
Bullet(int flySpeed){
this->flySpeed = flySpeed;
}
int getX() { return xLocation;}
int getY() { return yLocation;}
int getFlySpeed() { return flySpeed;}
bool getIsBetween() { return isBetween;}
void setX(int xLocation) { this->xLocation = xLocation;}
void setY(int yLocation) { this->yLocation = yLocation;}
void setIsBetween(bool isBetween) { this->isBetween = isBetween;}
};class MainCharacter{
private:
int xLocation;
int yLocation;
public:
MainCharacter() {}
int getX() { return xLocation;}
int getY() { return yLocation;}
void setX(int xLocation){ this->xLocation = xLocation;}
void setY(int yLocation){ this->yLocation = yLocation;}
};这就是我的代码应该是什么样子(简化)
#include <LiquidCrystal.h>
#define ... //something
class Bullet{//above};
class MainCharacter{//above};
int game(){ //something};
Bullet spawnBullet(int flySpeed, int locationY)
{
Bullet bl = Bullet(flySpeed);
bl.setY(locationY);
bl.setIsBetween(true);
bl.setX(LENGTH_X_MAX);
return bl;
}
void setup{
//something
static MainCharacter mc = MainCharacter();
mc.setX(1);
mc.setY(0);
}
void loop{
game();
}当我编译时,我收到错误'Bullet' does not name a type指向spawnBullet函数。我不明白为什么编译器不接受在这种情况下返回用户定义类的函数,而int game()可以工作。我已经尝试将类部分移动到#include之上或设置部分下(如果我删除了spawnBullet()函数,一切都会正常工作,因为您可以看到setup()下的MainCharacter构造函数调用仍然正常工作)。
非常感谢谁能指出我的错误。我已经为这个愚蠢的错误花了整整一个晚上,现在我已经绝望了。
这是我的Tinkercad项目的链接:https://www.tinkercad.com/things/fWqGvbNHamj-terrific-wluff/editel?sharecode=c2_J2v4jdWwJRrVpS7k3-LPJlEf_F5z082RSwWXUhY4
发布于 2021-02-16 05:12:06
我以前从未用过tinker cad,但我设法编译了你的项目。
以下是我对spawnBullet()声明的更改,这或多或少来自于C,尽管一些c++17功能,如自动在tinker cad中也可以工作。
class Bullet spawnBullet(int flySpeed, int locationY)
{
//...
}(经典的) Arduino项目的主.ino文件正在被预处理以生成.cpp文件,我认为问题来自于autodesk使用的预处理器。
我希望这样的错误不应该出现在tinker cad中,但这个变通方法至少应该允许你推进你的项目。这也可能与其他arduino预处理器不兼容,所以如果您将项目的开发转移到本地计算机上,您应该需要修改代码。
https://stackoverflow.com/questions/66213706
复制相似问题