[問題] varadic function編譯失敗
想寫一個最簡單的varadic function p(需不定數目的整數為參數),
把輸入的參數寫到standard out,
但是連編譯都有問題,找不到頭緒,
google找到的都是varadic template,但我不想用template,
可以請大家幫忙看一下哪裡錯了嗎?
(如果有違反板規麻煩告知,謝謝。)
//程式碼
#include <iostream>
void _real(int i) {
std::cout << i << ",";
}
void p(int head, int... tail) {
_real(head);
p(tail...);
}
void p() {};
int main() {
p(3,5,1,3,7,2);
}
//錯誤訊息
$ g++ -std=c++11 print.cpp
print.cpp:6:25: error: expansion pattern ‘int’ contains no argument packs
void p(int head, int... tail) {
^
print.cpp: In function ‘void p(int)’:
print.cpp:8:7: error: ‘tail’ was not declared in this scope
p(tail...);
^
print.cpp: In function ‘int main()’:
print.cpp:14:18: error: no matching function for call to ‘p(int, int, int, int, int, int)’
p(3,5,1,3,7,2);
^
print.cpp:14:18: note: candidates are:
print.cpp:6:6: note: void p(int)
void p(int head, int... tail) {
^
print.cpp:6:6: note: candidate expects 1 argument, 6 provided
print.cpp:11:6: note: void p()
void p() {};
^
print.cpp:11:6: note: candidate expects 0 arguments, 6 provided
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 106.105.12.35
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1490086381.A.C67.html
→
03/21 17:05, , 1F
03/21 17:05, 1F
→
03/22 08:07, , 2F
03/22 08:07, 2F
→
03/22 08:08, , 3F
03/22 08:08, 3F
→
03/22 08:08, , 4F
03/22 08:08, 4F
→
03/22 08:10, , 5F
03/22 08:10, 5F
推
03/22 16:11, , 6F
03/22 16:11, 6F
→
03/22 18:31, , 7F
03/22 18:31, 7F