class - 函数参数中的类(Arduino)无法编译

标签 class parameters compiler-errors arduino

我正在尝试在C++中创建一个简单的类,但是我不断收到编译错误:

main:2: error: variable or field 'doSomething' declared void
main:2: error: 'person' was not declared in this scope

主要:
class person {
  public:
    int a;
};

void doSomething(person joe) {
  return;
}

main()和其他东西会放在这里,但是即使我包含main(){},错误仍然会发生。我还尝试在joe之后添加2个封闭的括号,但这会产生错误:
main: In function 'void doSomething(person (*)())':
main:8: error: request for member 'a' in 'joe', which is of non-class type 'person (*)()'

任何帮助是极大的赞赏。 (我希望这不是我真正缺少的东西,因为我已经研究了几个小时)。

编辑:我发现这是一个特定于Arduino的错误。 This post回答。

最佳答案

在阅读this post之后我发现解决此问题的方法是:

typedef struct person{
public:
    int a;
};

void doSomething(void *ptr)
{  
    person *x;
    joe = (person *)ptr;
    joe->a = 3; //To set a to 3
    //Everything else is normal, except changing any value of person uses "->" rather than "."

    return;
}

main()
{
    person larry;
    doSomething(&larry);
}

所以本质上是:

-将class更改为typedef struct
-在参数中,将新类型替换为void *something
-将person *x;x = (person *)ptr;添加到函数的开头

-每当访问type属性时,请使用->而不是.

关于class - 函数参数中的类(Arduino)无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18304453/

相关文章:

c++ - 在同一对象的成员之间传递 ifstream 变量,C++

parameters - Ansible:覆盖 all.yml 中的值

compiler-errors - 编译FFT时使用“Undefined Reference…”(使用FFTW3库)

c++ - 树莓派上的 "error: stray\255 in program"

Php:单例VS全静态类?什么时候用什么?

c++ - 字符串作为参数 (C++)

c# - 创建具有任意参数的委托(delegate)

c++ - 模板类中的双嵌套类前向声明

jquery - 如何使用 jQuery 过滤不需要的元素