繁体   English   中英

使用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);

上面的函数模板不适用于类型的参数:

(int[999][20], int, int, int[20])

insert_ord函数的第一个参数是一个2D数组plate ,而最后一个是plate类型,它是一维数组。 您应该声明函数模板,如:

template <typename T, int sizeOuter, int sizeInner>
int insert_ord (T (&v)[sizeInner][sizeOuter], int ll, int const LF, T (&e)[sizeOuter]);

看到Live On Coliru


但是,你的代码有一些挑剔。 你不需要int main()中的void 如果可能的话,更喜欢constexpr超过#define 最重要的是,使用std::array<T, N>或考虑将数据结构包装在classstruct

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM