[英]Why does the compiler complains when `none const copy constructor` is used?
[英]Why does the compiler complains when I declare and define a constructor with no argument and a constructor with an argument?
我在C ++中有以下代碼:
#include <iostream>
class Object
{
public:
Object();
Object(int someValue = 0);
private:
int value;
};
Object::Object()
{
std::cout << "No argument constructor" << std::endl;
value = 0;
}
Object::Object(int someValue)
{
std::cout << "Argument constructor" << std::endl;
value = someValue
}
int main()
{
Object obj1; // should call Object() (according to me)
Object obj2(5); // should call Object(int) (according to me)
}
但是Windows 7 64位上的編譯器(MinGW 4.8.1)抱怨重載的Object()的調用是模棱兩可的:
defaultConstructorTest.cpp: In function 'int main()':
defaultConstructorTest.cpp:27:9: error: call of overloaded 'Object()' is ambiguous
Object obj1;
^
defaultConstructorTest.cpp:27:9: note: candidates are:
defaultConstructorTest.cpp:19:1: note: Object::Object(int)
Object::Object(int someValue)
^
defaultConstructorTest.cpp:13:1: note: Object::Object()
Object::Object()
^
因此,理想情況下,我想獲得以下輸出:
No argument constructor
Argument constructor
這是因為
Object obj1;
是big昧的。 由於第二個構造函數具有一個默認參數,這使其成為對象默認構造的足夠好的競爭者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.