[英]How can I convert optional parameter method declared in C into c++
我正在將一些舊的 C 代碼轉換為 C++ 並遇到一些奇怪的事情,.h 文件中聲明的 C 代碼說: void Message(char *s, ...);
和 .c 中的實現頭
void Message(char *s, long x1, long x2, long x3, long x4, long x5, long x6, long x7, long x8, long x9, long x10){...}
當我在 C++ 中將“直接”復制到類聲明和實現中時,我得到“原型錯誤”。
我是否需要像在 .c 文件中那樣做一個完整的聲明,或者還有另一種 C++ 和類使用的方法嗎? x1 ... x10 是選項參數,很少使用。
使用 g++ 和 NetBeans 7.3 在 linux 中工作
C 代碼不應該編譯。 要在 C++ 中實現可選參數,請將它們的值放在聲明中:
void Message(char *s, long x1 = 0L, long x2 = 0L, long x3 = 0L, long x4 = 0L, long x5 = 0L,
long x6 = 0L, long x7 = 0L, long x8 = 0L, long x9 = 0L, long x10 = 0L);
然后像這樣定義你的函數:
void Message(char *s, long x1, long x2, long x3, long x4, long x5,
long x6, long x7, long x8, long x9, long x10)
{
// Function body
}
請注意,您只需在一處指定默認值。 在聲明中執行此操作,以便客戶可以看到它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.