2016-11-11 90 views
-3

我試圖創建對象的BuyOrder創建對象的數組,沒有匹配的構造函數初始化

BuyOrder buy[10]; 

爲什麼我收到錯誤的數組的錯誤說「BuyOrder [10]無匹配的構造函數初始化」?

以下是BuyOrder的構造函數。我是否必須創建另一個默認構造函數?

BuyOrder::BuyOrder(double price, int quantity, Stock &s) 
    :buyPrice{ price }, 
    buyQuantity{quantity}, 
    buyStock{ s } 
    {} 
+1

這不是默認構造函數。默認的構造函數不帶任何參數。 –

+1

您可以嘗試創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)並向我們展示? –

+0

@MichaelAlbers所以你的意思是我只能聲明一個對象的數組,其中的對象有默認的構造函數? –

回答

0

就像我在我的評論中說過的,你can做聚合初始化小數組。

#include <array> 

struct example 
{ 
    example(int, double) {} 
    example(example const&) = delete; 
}; 

int main() { 

    example arr1[2] { 
     {1, 3.4}, 
     {2, 5.6} 
    }; 

    std::array<example, 2> arr2 {{ 
     {1, 3.4}, 
     {2, 5.6} 
    }}; 

    return 0; 
} 
相關問題