2014-01-22 216 views
1

我創建了1個帶有2個類的庫。 Class Wave和Class LEDLamps。在第二個類的構造函數中,我試圖在沒有任何運氣的情況下填充第一類對象的數組。Arduino:初始化構造函數中的自定義對象

這是我的真實代碼的一些部分。 .h文件:

static const int numberOfWaves = 20; 

class Wave 
{ 
public: 
    Wave(int speed, int blockSize, int ledCount, int lightness,int startCount); // Constructor 

private: 

}; 

// ------------------------------------------------------------------------------------------- // 
class LEDLamps 
{ 
public: 
    LEDLamps(int8_t lampCount, int8_t dataPin, int8_t clockPin); //Constructor 

private: 
    Wave waveArray[numberOfWaves]; 
}; 

.cpp文件

Wave::Wave(int speed, int blockSize, int ledCount, int lightness, int startCount) //Constructor 
{ 
      // Doing some stuff... 
} 

// ------------------------------------------------------------------------------------------- // 
LEDLamps::LEDLamps(int8_t lampCount, int8_t dataPin, int8_t clockPin) //Constructor 
{ 
    int i; 
    for (i = 0; i < numberOfWaves; i++) { 
     waveArray[i] = Wave(10,2,25,150,100); 
    } 
} 

錯誤消息:

LEDLamps.cpp: In constructor 'LEDLamps::LEDLamps(int8_t, int8_t, int8_t)': 
LEDLamps.cpp:66: error: no matching function for call to 'Wave::Wave()' 
LEDLamps.cpp:14: note: candidates are: Wave::Wave(int, int, int, int, int) 
LEDLamps.h:23: note:     Wave::Wave(const Wave&) 

我從錯誤消息中的參數錯誤理解什麼,但我要送5的整數和構造函數被定義爲接收5個整數?所以我一定是別的我做錯了...

回答

2

該錯誤告訴你到底什麼錯,沒有Wave::Wave()方法。您需要Wave類的默認構造函數才能創建它的數組。如果Wave類包含非平凡數據,您可能還想創建一個複製分配操作符。

的問題是,該數組身體LEDLamps構造運行的,所以之前建造LEDLamps構造體內的陣列完全構建,以及你正在做的是轉讓(使用自動生成的禁止複製賦值運算符)。


不幸的是,默認的Arduino C++庫非常有限,至少當涉及到「標準」C++特性時。有libraries that helps,如果有可能使用這樣的庫,你也可以使用一個std::vector代替,這樣可以讓你構建其載體在構造函數初始化列表:

class LEDLamps 
{ 
    ... 
    std::vector<Wave> waveVector; 
}; 

... 

LedLamps::LEDLamps(...) 
    : waveVector(numberOfWaves, Wave(10,2,25,150,100)) 
{ 
} 
相關問題