我這樣fun重載竟然報錯了。。改成double就行,但是float就不行!什么鬼??
#include"iostream" using?namespace?std; void?fun(int?x?=?1,int?y?=?2,int?z?=?3); void?fun(float?x,float?y); int?main() { fun(0.9,0.1); system("pause"); return?0; } void?fun(int?x,int?y,int?z) { cout?<<x<<','<<y<<','<<z<<endl; } void?fun(float?x,float?y) { cout<<x<<','<<y<<endl; }
error C2668: 'fun' : ambiguous call to overloaded function
could be 'void fun(float,float)'
or ? ? ? 'void fun(int,int,int)'
2016-01-22
雖然0.9既能算是double又能算是float但是系統(tǒng)默認0.9這類的數(shù)為double型的,所以你要用float需要在數(shù)前指明。你的fun(0.9,0.1);函數(shù)里的0.9和0.1數(shù)被系統(tǒng)默認為double型的,所以在尋找double型重載時不匹配故報錯,你要這樣就可以:fun((float)0.9,(float)0.1);
2016-01-20
其實好多 ?float型 ?都能用 double型表示 ? 或許這和 編譯器有關