Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 变量模板未编译_C++_C++11_Variadic Templates - Fatal编程技术网

C++ 变量模板未编译

C++ 变量模板未编译,c++,c++11,variadic-templates,C++,C++11,Variadic Templates,代码: #包括 模板 void out()//第4行 { } 模板 void out(T值,Args…Args)//第9行 { std::cout有一个语法错误(错误的),使编译器发疯 在此之后,您可能还会遇到out(“12345”,std::endl)调用的问题,因为std::endl是编译器无法选择的重写函数(只需将其转换为static\u cast(std::endl)) 另外,out中的递归以out()调用结束,但是没有带0个参数的out。(另请参见yuri answer:)问题是您将o

代码:

#包括
模板
void out()//第4行
{
}
模板
void out(T值,Args…Args)//第9行
{
std::cout有一个语法错误(错误的
),使编译器发疯

在此之后,您可能还会遇到
out(“12345”,std::endl)
调用的问题,因为
std::endl
是编译器无法选择的重写函数(只需将其转换为
static\u cast(std::endl)


另外,
out
中的递归以
out()
调用结束,但是没有带0个参数的out。(另请参见yuri answer:)

问题是您将
out
的无参数版本设置为模板,并且在不显式提供模板参数的情况下无法调用它。因此

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:17:24: error: no matching function for call to ‘out(const char [6], <unresolved overloaded function type>)’
../main.cpp:17:24: note: candidates are:
../main.cpp:4:6: note: template<class T> void out()
../main.cpp:4:6: note:   template argument deduction/substitution failed:
../main.cpp:17:24: note:   candidate expects 0 arguments, 2 provided
../main.cpp:9:6: note: void out(T, Args ...) [with T = const char*; Args = {}]
../main.cpp:9:6: note:   candidate expects 1 argument, 2 provided
void out(){}
模板
作废(T值,参数…参数){

std::难道你在解包语句中缺少一个
。将其更改为
args…
。编辑问题时,还有另一条消息带有…@AlexFarber我的歉意…谢谢。
是打字错误,已修复。从调用中删除
std::endl
没有帮助,我想问题在于模板本身。删除后首先,模板看起来更好。你是对的,
std::endl
没有编译。但是你的转换不起作用:
无效的静态转换从类型“”转换为类型std::ostream&(std::ostream&){aka std::basic_ostream&(std::basic_ostream&}
OOPS:查看编辑(忘记了
(*)
)谢谢。我贴了另一个问题,把答案写在那里。你要找的词是“论点类型推断”。
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:17:24: error: no matching function for call to ‘out(const char [6], <unresolved overloaded function type>)’
../main.cpp:17:24: note: candidates are:
../main.cpp:4:6: note: template<class T> void out()
../main.cpp:4:6: note:   template argument deduction/substitution failed:
../main.cpp:17:24: note:   candidate expects 0 arguments, 2 provided
../main.cpp:9:6: note: void out(T, Args ...) [with T = const char*; Args = {}]
../main.cpp:9:6: note:   candidate expects 1 argument, 2 provided
void out() {}

template<typename T, typename... Args>
void out(T value, Args... args) {
    std::cout << value;
    out(args...);
}

int main() {
    out(1, 2.0, "3");
    return 0;
}