使用 typename 并将 typedef 传递给函数的 C++ 模板
C++ Template using typename and passing typedef to function
我正在以这种方式使用模板。
#define MAX_CARS 999
typedef char plate[20];
template <typename T>
int insert_ord (T V[], int ll, int const LF, T e);
它有效,但是当我将 typedef 作为参数传递时,它说:没有匹配的函数来调用 "insert_ord"。
这是主要的。
int main (void)
{
plate targhe[MAX_CARS];
int ll = 0;
plate targa;
cin >> targa;
ll = insert_ord(targhe, ll, MAX_CARS, targa);
cout << endl;
system("pause");
return 0;
}
template <typename T>
int insert_ord (T V[], int ll, int const LF, T e);
上面的 function-template 不适用于以下类型的参数:
(int[999][20], int, int, int[20])
insert_ord
函数的第一个参数是 plate
的二维数组,而最后一个参数是 plate
类型的一维数组。您应该像这样声明 function-template:
template <typename T, int sizeOuter, int sizeInner>
int insert_ord (T (&v)[sizeInner][sizeOuter], int ll, int const LF, T (&e)[sizeOuter]);
但是,您的代码存在一些问题。 int main()
中不需要 void
。如果可能,更喜欢 constexpr
而不是 #define
。最重要的是,使用 std::array<T, N>
或考虑将数据结构包装在 class
或 struct
中
我正在以这种方式使用模板。
#define MAX_CARS 999
typedef char plate[20];
template <typename T>
int insert_ord (T V[], int ll, int const LF, T e);
它有效,但是当我将 typedef 作为参数传递时,它说:没有匹配的函数来调用 "insert_ord"。
这是主要的。
int main (void)
{
plate targhe[MAX_CARS];
int ll = 0;
plate targa;
cin >> targa;
ll = insert_ord(targhe, ll, MAX_CARS, targa);
cout << endl;
system("pause");
return 0;
}
template <typename T>
int insert_ord (T V[], int ll, int const LF, T e);
上面的 function-template 不适用于以下类型的参数:
(int[999][20], int, int, int[20])
insert_ord
函数的第一个参数是 plate
的二维数组,而最后一个参数是 plate
类型的一维数组。您应该像这样声明 function-template:
template <typename T, int sizeOuter, int sizeInner>
int insert_ord (T (&v)[sizeInner][sizeOuter], int ll, int const LF, T (&e)[sizeOuter]);
但是,您的代码存在一些问题。 int main()
中不需要 void
。如果可能,更喜欢 constexpr
而不是 #define
。最重要的是,使用 std::array<T, N>
或考虑将数据结构包装在 class
或 struct