[英]Addition of default argument on redeclaration makes this constructor a default constructor
它說在重新聲明中添加default參數使此構造函數成為默認構造函數。
我對此進行了一些研究,但我只是不明白該如何解決該問題。
struct Transaction{
int type;
int amount;
int to_from_type;
Transaction(int, int, int);
};
Transaction :: Transaction(int type=0, int amount=0, int etc=0)
{
this->type=type;
this->amount=amount;
this->to_from_type=etc;
}
Transaction :: Transaction(int type=0, int amount=0, int etc=0) //I am getting an error at this code and I cannot fix it.
{
this->type=type;
this->amount=amount;
this->to_from_type=etc;
}
我不是C ++專家,很想在我的代碼方面獲得一些幫助。
XCode使用CLang和Apple LLVM的組合來編譯您的C ++代碼。 Clang會執行一些其他檢查,包括您的案件。 這里發生的事情是您定義了一個接受3個參數的構造函數,但是您的實現可以不加任何調用,這意味着,您實現的實際上與隱式默認構造函數帶有相同的方法簽名(方法名稱和參數列表),而帶有3個參數的params(在結構內部定義的參數)在編譯器的視野中被遺漏了。 解決方法很簡單:
struct Transaction{
int type;
int amount;
int to_from_type;
Transaction(int=0, int=0, int=0);
};
Transaction :: Transaction(int type, int amount, int etc)
{
this->type=type;
this->amount=amount;
this->to_from_type=etc;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.