C++ 函数模板参数编译错误
我正在尝试这样做:C++ 函数模板参数编译错误,c++,visual-studio-2010,c++11,C++,Visual Studio 2010,C++11,我正在尝试这样做: #include <iostream> #include <array> using namespace std; template <size_t A> class Test { public: typedef array<int, A> TestType; }; template <size_t A> void foo(Test<A>::TestType t) {
#include <iostream>
#include <array>
using namespace std;
template <size_t A>
class Test {
public:
typedef array<int, A> TestType;
};
template <size_t A>
void foo(Test<A>::TestType t) {
cout << "test\n";
}
int main() {
Test<5>::TestType q;
foo(q);
return 0;
}
template <size_t A>
void foo(typename Test<A>::TestType t) {
cout << "test\n";
}
我不明白我做错了什么,因为A是一个编译时常数。我应该更改什么?如果您要像这样添加
typename
:
#include <iostream>
#include <array>
using namespace std;
template <size_t A>
class Test {
public:
typedef array<int, A> TestType;
};
template <size_t A>
void foo(Test<A>::TestType t) {
cout << "test\n";
}
int main() {
Test<5>::TestType q;
foo(q);
return 0;
}
template <size_t A>
void foo(typename Test<A>::TestType t) {
cout << "test\n";
}
q
的类型是std::array
,编译器不知道该类型如何连接到Test
。在调用foo(q)
时,需要对未标准化的代码进行更深入的分析,以找出a
只有一个可能的匹配项。你需要打电话
foo<5>(q);
如果您要像这样添加
typename
:
#include <iostream>
#include <array>
using namespace std;
template <size_t A>
class Test {
public:
typedef array<int, A> TestType;
};
template <size_t A>
void foo(Test<A>::TestType t) {
cout << "test\n";
}
int main() {
Test<5>::TestType q;
foo(q);
return 0;
}
template <size_t A>
void foo(typename Test<A>::TestType t) {
cout << "test\n";
}
q
的类型是std::array
,编译器不知道该类型如何连接到Test
。在调用foo(q)
时,需要对未标准化的代码进行更深入的分析,以找出a
只有一个可能的匹配项。你需要打电话
foo<5>(q);
它必须是
typename Test::TestType
,因为它是一个依赖名称。它必须是typename Test::TestType
,因为它是一个依赖名称。