Tinkercad:函数 return user-defined class "does not name type" in Arduino
Tinkercad: Function return user-defined class "does not name type" in Arduino
我一直在使用 Tinkercad 的模拟器在 Arduino Uno 上编写一个简单的游戏。我创建了一个名为 class 的 Bullet 和一个名为 class 的 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
函数。我不明白为什么编译器在这种情况下不接受返回用户定义 class 的函数,而 int game()
可以工作。我试图将 class 部分四处移动,在 #include 上方或设置部分下方(如果我删除 spawnBullet()
函数,一切正常,正如您可以看到 MainCharacter 构造函数调用setup()
仍然运行良好)。
非常感谢谁能指出我的错误。我为这个愚蠢的错误花了整整一个晚上,现在我绝望了。
这是我的 Tinkercad 项目的 link:https://www.tinkercad.com/things/fWqGvbNHamj-terrific-wluff/editel?sharecode=c2_J2v4jdWwJRrVpS7k3-LPJlEf_F5z082RSwWXUhY4
我以前从未使用过 tinker cad,但我设法编译了你的项目。
这是我对 spawnBullet() 声明的更改,这或多或少来自 C,尽管某些 c++17 功能(如 auto)在 tinker cad 中工作。
class Bullet spawnBullet(int flySpeed, int locationY)
{
//...
}
(经典)Arduino项目的主.ino文件正在预处理生成.cpp文件,我认为问题出在autodesk使用的预处理器上。
我希望 tinker cad 中不会出现这样的错误,但此变通方法至少应该允许您推进您的项目。这也可能与其他 arduino 预处理器不兼容,因此如果您将项目开发转移到本地计算机,您应该期望必须修改代码。
我一直在使用 Tinkercad 的模拟器在 Arduino Uno 上编写一个简单的游戏。我创建了一个名为 class 的 Bullet 和一个名为 class 的 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
函数。我不明白为什么编译器在这种情况下不接受返回用户定义 class 的函数,而 int game()
可以工作。我试图将 class 部分四处移动,在 #include 上方或设置部分下方(如果我删除 spawnBullet()
函数,一切正常,正如您可以看到 MainCharacter 构造函数调用setup()
仍然运行良好)。
非常感谢谁能指出我的错误。我为这个愚蠢的错误花了整整一个晚上,现在我绝望了。
这是我的 Tinkercad 项目的 link:https://www.tinkercad.com/things/fWqGvbNHamj-terrific-wluff/editel?sharecode=c2_J2v4jdWwJRrVpS7k3-LPJlEf_F5z082RSwWXUhY4
我以前从未使用过 tinker cad,但我设法编译了你的项目。
这是我对 spawnBullet() 声明的更改,这或多或少来自 C,尽管某些 c++17 功能(如 auto)在 tinker cad 中工作。
class Bullet spawnBullet(int flySpeed, int locationY)
{
//...
}
(经典)Arduino项目的主.ino文件正在预处理生成.cpp文件,我认为问题出在autodesk使用的预处理器上。
我希望 tinker cad 中不会出现这样的错误,但此变通方法至少应该允许您推进您的项目。这也可能与其他 arduino 预处理器不兼容,因此如果您将项目开发转移到本地计算机,您应该期望必须修改代码。