【问题标题】:'ControllerPosition' does not name a type“ControllerPosition”没有命名类型
【发布时间】:2016-02-29 16:44:49
【问题描述】:

在以下代码中,avr-g++ (Arduino IDE) 编译器抛出错误:'ControllerPosition' does not name a type。代码有什么问题?

struct ControllerPosition
{
  int y, x;

  ControllerPosition(int _y = 0x7FFF, int _x = 0x7FFF) : y(_y), x(_x) {}
};

ControllerPosition mapPosition(int input)
{
  return ControllerPosition((input % 10) * 2 + 1, (input / 10) * 2 + 1);
}

【问题讨论】:

  • Your problem is in another castle. 确定你没有遗漏 include 语句之类的吗?
  • 错误信息是指哪一行?
  • 是的。如果将其粘贴到 Arduino IDE 的简单文档中,将无法正确编译。
  • 第2行,错误码具体为:sketch_feb29a.ino:2:1: error: 'ControllerPosition' does not name a type

标签: c++ arduino arduino-ide


【解决方案1】:

我不确定发布的代码有问题。以下对我有用:

struct ControllerPosition {
    int y,x;

    ControllerPosition(int _y = 0x7FFF,int _x = 0x7FFF) : y(_y),x(_x) {}
};

ControllerPosition mapPosition(int input)
{
    return ControllerPosition((input % 10) * 2 + 1,(input / 10) * 2 + 1);
}

int main()
{
    auto testvar = mapPosition(4);
    return 0;
}

查看this post 以更通用的术语讨论类似错误。

【讨论】:

  • 你用过avr-g++编译器吗?
  • 不,我使用了 MSVC。我添加了一个指向 Arduino 特定的更通用答案的链接。
【解决方案2】:

我找到了问题的根源。 Arduino IDE 在给定文件中查找函数将它们的原型放在文件的开头,这意味着它将原型放在结构定义之前,并且函数使用结构作为其返回类型。因此解决方案是在结构定义之后显式声明原型。

struct ControllerPosition 
{
  int y;
  int x;
};

ControllerPosition mapPosition(int input);

ControllerPosition mapPosition(int input)
{
  return ControllerPosition((input % 10) * 2 + 1, (input / 10) * 2 + 1);
}

PS:可以通过类前向声明​​struct ControllerPosition;在结构体之前声明原型。

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 2023-04-07
    • 2021-02-03
    • 2017-04-01
    • 2015-06-09
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多