struct structA
{
StructA(const int a) { ... } ;
}
,然後我的主要結構:C++構造不匹配功能
.H
struct MainStruct
{
MainStruct(int x, int y) ;
private :
int _x ;
int _y ;
StructA _s ;
}
*的.cpp
StructA(int x, int y) : _x(x) , _y(y)
{
_s = StructA(x) ;
}
有什麼不對?
如果我將_s = StructA(x) ;
替換爲StructA s = StructA(x) ;
,並將其從私人中刪除,則可以正常工作。這是爲什麼?
In constructor ....
no matching function for call to 'StructA'
_y(y)
在您輸入構造函數的主體之前,所有成員都必須完全構造。 '_s'不能被構造,因爲它沒有在成員初始化列表中用適當的參數指定,並且沒有默認的構造函數。 – user4581301
這是由於這樣的事實,即當您使用參數聲明構造函數時,缺省構造函數將在默認情況下被移除(然後您必須明確聲明它)。 –