foo(int (*fnptr)(int));
我想提出一個默認值的函數指針int bar(int)
即指針的默認值是bar
bar
也過載爲
double bar (double);
bool bar (bool);
我該如何賦值?
我試圖
foo (int (*fnptr)(int) = bar);
,但它不工作。
編輯我使用MS Visual Studio和得到錯誤代碼C2440
'默認參數':無法從 '重載函數' 到 'Error_C(__cdecl *)(HMstd ::例外)' 轉換
我的實際功能是我定義的命名空間HMstd
virtual Error_C execute_protocol(Error_C(*execute)(exception ex) = HMstd::MErr);
的exception
類的成員函數和函數是
Error_C MErr(Error_C code);
Error_C MErr(char* desc);
Error_C MErr(exception ex);
其中Error_C
另一類
這是三個重載函數的定義HMstd::MErr
是
Error_C HMstd::MErr(Error_C code)
{
std::cout << "\n\nError: An Error Of Code " << int(code) << " Occured....\n\n";
return SUCCESS;
}
Error_C HMstd::MErr(char* desc)
{
if (desc == NULLPTR)
return E_NULLPTR;
std::cout << desc;
return SUCCESS;
}
Error_C HMstd::MErr(exception ex)
{
bool Nullar = TRUE;
bool uninit;
for (int i = 0;i < 200;i++)
if (ex.description[i] != '\0')
Nullar = FALSE;
uninit = (int(ex.code) == -201) && Nullar;
if (uninit)
{
return UNINIT_PARAMETER;
}
MErr(ex.code);
MErr(ex.description);
return SUCCESS;
}
你有什麼錯誤?我看起來很好。 – Quentin
編譯這樣的代碼我沒有問題 - 你可以提供[MCVE](http://stackoverflow.com/help/mcve)嗎?你正在使用哪種編譯器/版本/平臺? – Holt
是的。 'int bar(int);雙重酒吧(雙人);布爾吧(布爾); void foo(int(* fnptr)(int)= bar); int main(){}'爲我編譯並運行。 –