2017-07-26 213 views
0

我能够产生一个可编译的代码片段,将结构传递给函数,但是当我试图使用'传值'时,我的代码就崩溃了。我怎样才能通过值的文件通过结构

我已经看过如何跨多个文件使用相同的格式化结构,但我不知道按值传递函数时它是否有任何不同?

注:这是写在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&)’ 

什么想法?

+1

'__a_h'是被保留以执行的标识符。通过定义它,你谴责你的程序有未定义的行为。 – user2079303

+0

这个问题似乎与C语言无关。 – user2079303

+0

你的'a.h'是否有'addData'的值超载原型? – SergeyA

回答

1

C++将为结构和类生成默认构造函数和复制运算符。

为了您MYSTRUCT这些隐函数是:

struct myStruct { 

    myStruct() {}        // <-- implicit default constructor. 

    myStruct(const myStruct& other)    // <-- implicit copy contructor 
    { 
    foo = other.foo; 
    bar = other.bar; 
    } 

    myStruct& operator = (const myStruct& other) // <-- implicit copy operator 
    { 
    foo = other.foo; 
    bar = other.bar; 
    return *this; 
    } 

    int foo; 
    int bar; 
}; 

注意,拷贝构造函数和运营商期望const myStruct&参数。 myStruct volatile structure1;定义中的volatile关键字可防止参数匹配。

您必须显式声明接受const volatile myStruct&的复制运算符和/或构造函数才能编译代码。

volatile数据需要由编译器的优化器进行特殊处理。这就是为什么volatile关键字在这里很重要。你应该真的考虑你的数据是否真的需要这个限定符。在Arduino中,只有一种情况需要使用这个关键字,即当数据是通过中断例程修改时。

或者,也可以定义接受挥发性引用或指针数据功能:

struct MyStruct // I suggest you use this syntax for declarting structures 
{     // So you don't ghave to repeat the struct keyword everywhere. 
    myStruct(const myStruct& other) 
    { 
     foo = other.foo; 
     bar = other.bar; 
    } 
    myStruct(const volatile myStruct& other) 
    { 
     foo = other.foo; 
     bar = other.bar; 
    } 
    int foo, bar; 
}; 

int addData(volatile const myStruct* structureC) 
{ 
    return structureC->foo + structureC->bar; 
} 

int addData(volatile const myStruct& structureC) 
{ 
    return structureC.foo + structureC.bar; 
} 

int addDataByCopy(myStruct structureC) 
{ 
    return structureC.foo + structureC.bar; 
} 

// ... 
volatile myStruct my; 
void loop() 
{ 
    my.foo = 1; 
    my.bar = 1; 
    int x = addData(my); // by const reference. 
    // or 
    int y = addData(&my); // by pointer. 
    // or 
    int z = addDataByCopy(my); // by copy 
} 
+0

这真的很有用!然而,你能提供一个例子,我将如何将结构传递给'传值'和'传递地址'格式的函数吗? –