我能够产生一个可编译的代码片段,将结构传递给函数,但是当我试图使用'传值'时,我的代码就崩溃了。我怎样才能通过值的文件通过结构
我已经看过如何跨多个文件使用相同的格式化结构,但我不知道按值传递函数时它是否有任何不同?
注:这是写在Arduino的IDE中的C++
我通过地址传递代码如下:
passingStructs.ino
#include "a.h"
#include "b.h"
myStruct volatile structure1;
void setup() {
}
void loop() {
structure1.foo = 7;
structure1.bar = 11;
int lower = minusData(&structure1);
int higher = addData(&structure1);
}
啊:
#include "b.h"
#ifndef __a_h
#define __a_h
//prototype functions
int addData(struct myStruct *structureC);
#endif //__a_h
a.cpp:
#include "a.h"
#include "b.h"
int addData(struct myStruct *structureC) {
int x = structureC->foo;
int y = structureC->bar;
return (x + y);
}
b.h:
#ifndef __b_h
#define __b_h
//Define structure
typedef struct myStruct {
int foo;
int bar;
};
//Prototype functions
int minusData(struct myStruct *structureC);
#endif //__b_h
b.cpp:
#include "b.h"
myStruct structureC;
int minusData(struct myStruct *structureC) {
int x = structureC->foo;
int y = structureC->bar;
return (x - y);
}
然而如果我使用 INT更高=广告
DDATA(structure1); 在.ino文件和
int addData(struct myStruct structureC) {
int x = structureC.foo;
int y = structureC.bar;
return (x + y);
}
与相同的原型在头文件中的a.cpp文件,编译器会拒绝代码说
no matching function for call to ‘myStruct::myStruct(volatile myStruct&)’
什么想法?
'__a_h'是被保留以执行的标识符。通过定义它,你谴责你的程序有未定义的行为。 – user2079303
这个问题似乎与C语言无关。 – user2079303
你的'a.h'是否有'addData'的值超载原型? – SergeyA