[英]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.