小编use*_*004的帖子

在类模板中调用模板成员函数时未解析的重载函数类型

考虑以下代码:

\n\n
struct Test {\n    template <int S>\n    bool call();\n};\n\ntemplate <>\nbool Test::call<0>() {\n    return false;\n}\n\ntemplate <>\nbool Test::call<1>() {\n    return true;\n}\n\ntemplate <int S, typename T>\nstatic void func(T& t) {\n    t.call<S>();\n}\n\nint main()\n{\n    Test t;\n    func<0>(t);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我遇到编译错误:

\n\n
a.cpp: In function \xe2\x80\x98void func(T&)\xe2\x80\x99:\na.cpp:19:15: error: expected primary-expression before \xe2\x80\x98)\xe2\x80\x99 token\na.cpp: In instantiation of \xe2\x80\x98void func(T&) [with int S = 0; T = Test]\xe2\x80\x99:\na.cpp:25:14:   required from here\na.cpp:19:5: error: invalid operands of types \xe2\x80\x98<unresolved overloaded function type>\xe2\x80\x99 and \xe2\x80\x98int\xe2\x80\x99 to binary \xe2\x80\x98operator<\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我把t.call<0>() …

c++ templates

3
推荐指数
1
解决办法
948
查看次数

标签 统计

c++ ×1

templates ×1