2017-07-26 71 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

這真的很有用!然而,你能提供一個例子,我將如何將結構傳遞給'傳值'和'傳遞地址'格式的函數嗎? –

相關問題