[英]C++11 for loop in a Template Function
我正在尝试编写一个在控制台上打印数据的功能。 该函数是模板化的,因为它应该接受不同类型的数据。
代码如下所示:
template<typename DataType>
void PrintData(DataType *X)
{
for (DataType Data : X)
{
cout << Data << "\t";
}
cout << endl;
}
int main()
{
int nArray[7] = { 7, 5, 4, 3, 9, 8, 6 };
double dArray[5] = { 4.3, 2.5, -0.9, 100.2, 3.0 };
PrintData(nArray);
PrintData(dArray);
system("pause");
return EXIT_SUCCESS;
}
我得到一个错误,即模板化函数PrintData中的变量Data未声明 。
error C2065: 'Data' : undeclared identifier
error C3312: no callable 'begin' function found for type 'double *'
error C3312: no callable 'begin' function found for type 'int *'
error C3312: no callable 'end' function found for type 'double *'
error C3312: no callable 'end' function found for type 'int *'
任何帮助,将不胜感激。 谢谢
假设您已经包含了iostream
头文件和using namespace std;
指示。 那么你的问题是:
DataType *
。 你的代码使X
成为一个指针,它与数组不同。 请改用DataType const&
或DataType&&
。 iterator
头文件,该文件为C样式数组提供begin
和end
函数。 以下代码适用于我。
#include <iostream>
#include <iterator>
using namespace std;
template<typename DataType>
void PrintData(DataType const& X)
{
for (auto Data : X)
{
cout << Data << "\t";
}
cout << endl;
}
int main()
{
int nArray[7] = { 7, 5, 4, 3, 9, 8, 6 };
double dArray[5] = { 4.3, 2.5, -0.9, 100.2, 3.0 };
PrintData(nArray);
PrintData(dArray);
return EXIT_SUCCESS;
}
正如Igor Tandetnik评论的那样,如果要引用数组的大小,可以使用template<struct DataType, size_t N>
。
更新:
template<struct DataType>
和DataType *X
, DataType
被推导为int
和double
,而X
是不是容器的指针。 template<struct DataType, size_t N>
和DataType (&X)[N]
, DataType
被推导为int
和double
,而X
是一个可以与基于范围的for循环一起使用的数组。 template<struct DataType>
和DataType&& X
或DataType const& X
, DataType
被推导为int[7]
或double[5]
, X
也是一个数组。 问题是你传递的是数组的名称(例如, nArray
),它只是一个指向第一个元素的指针。 但是,新式for
循环期望您可以调用begin
和end
- 一个范围。
以下更改使其可以使用vector
而不是数组。 请注意,除此之外几乎没有什么区别,并且通常很少有理由在当代C ++中使用C风格的数组。
#include <iostream>
#include <vector>
using namespace std;
template<typename DataType>
void PrintData(const DataType &X)
{
for (const auto &Data : X)
{
cout << Data << "\t";
}
cout << endl;
}
int main()
{
vector<int> nArray = { 7, 5, 4, 3, 9, 8, 6 };
vector<double> dArray = { 4.3, 2.5, -0.9, 100.2, 3.0 };
PrintData(nArray);
PrintData(dArray);
system("pause");
return EXIT_SUCCESS;
你有一些问题:
1)DataType必须是容器。 所以,尝试使用:
std::vector<int> nArray = { 7, 5, 4, 3, 9, 8, 6 };
std::vector<double> dArray = { 4.3, 2.5, -0.9, 100.2, 3.0 };
2)你不会改变容器。 最好通过const引用传递容器:
template<typename DataType>
void PrintData(const DataType & X);
3)像这样改变循环:
for (const auto & value : X) {
std::cout << value << "\t";
}
代码示例:
#include <iostream>
#include <vector>
template<typename DataType>
void PrintData(const DataType & X) {
for (const auto & value : X) {
std::cout << value << "\t";
}
std::cout << std::endl;
}
int main() {
std::vector<int> nArray = { 7, 5, 4, 3, 9, 8, 6 };
std::vector<double> dArray = { 4.3, 2.5, -0.9, 100.2, 3.0 };
PrintData(nArray);
PrintData(dArray);
return 0;
}
正如Igor建议的那样,如果你想使用DataType *
那么你需要做这样的事情
#include <iostream>
#include <iterator>
using namespace std;
template <typename DataType, size_t N>
void PrintData(DataType (&X)[N])
{
for (auto i : X)
cout << i << "\t";
cout << endl;
}
int main()
{
int nArray[7] = { 7, 5, 4, 3, 9, 8, 6 };
double dArray[5] = { 4.3, 2.5, -0.9, 100.2, 3.0 };
PrintData(nArray);
PrintData(dArray);
return EXIT_SUCCESS;
}
产量
7 5 4 3 9 8 6
4.3 2.5 -0.9 100.2 3
说明:
如果你看到void PrintData(int* nArray);
& void PrintData(int (&nArray)[7] );
是类似的声明,除了秒一告诉数组结束的位置。
模板功能
template <typename DataType, size_t N>
void PrintData(DataType (&X)[N])
被推断为'
void PrintData(int (&nArray)[7] )
你也可以写
void PrintData(int (&nArray)[7] )
{
for (auto i : nArray)
cout << i << "\t";
cout << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.