error: no matching function for call to 'begin(long double [nPoints])' ; initializing vector with a hardcoded int versus an integer variable

error: no matching function for call to 'begin(long double [nPoints])' ; initializing vector with a hardcoded int versus an integer variable

我编写了一个使用幻数的代码,现在我正试图将幻数放入变量中。本质上,我创建了一个包含固定数量元素的数组,填充数组,然后将其转换为向量。这有效,只要我将数组创建为:

long double yy[5];
vector<long double> YY;
YY.insert(YY.begin(), begin(yy), end(yy));

但如果我将数组创建为:

,则数组转换会中断
int nPoints = 5;
long double yy[nPoints];
vector<long double> YY;
YY.insert(YY.begin(), begin(yy), end(yy));

我已经尝试将 nPoints 转换为其他数据类型,检查以确保它是我分配给它的数据类型等,但我没有任何运气。

// non functional code
#include <iostream>
#include <vector>

using namespace std;

int main(){
    int nPoints = 5;

    // initialize an input vector
    vector<long double> xx = {1, 2, 3, 4, 5};

    // compute some function of xx in a loop
    long double yy[nPoints];
    for(int i = 0; i<=nPoints - 1; i++){
        yy[i] = 2*xx[i];
    }

    // convert yy from an array to a vector
    vector<long double> YY;
    YY.insert(YY.begin(), begin(yy), end(yy));

    for(int i = 0; i<=nPoints -1; i++){
        cout<<YY[i]<<endl;
    }
    return 0;
}

和工作代码

// Working Code: simply changed long double yy[nPoints] to long double yy[5]

#include <vector>

using namespace std;

int main(){
    int nPoints = 5;

    // initialize an input vector
    vector<long double> xx = {1, 2, 3, 4, 5};

    // compute some function of xx in a loop
    long double yy[5];
    for(int i = 0; i<=nPoints - 1; i++){
        yy[i] = 2*xx[i];
    }

    // convert yy from an array to a vector
    vector<long double> YY;
    YY.insert(YY.begin(), begin(yy), end(yy));

    for(int i = 0; i<=nPoints -1; i++){
        cout<<YY[i]<<endl;
    }
    return 0;
}

第一个returns错误信息如下:

g++ -Wall testMain.cpp
testMain.cpp: In function 'int main()':
testMain.cpp:20:32: error: no matching function for call to 'begin(long double [nPoints])'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
In file included from c:\mingw\include\c++.1.0\bits\range_access.h:36,
                 from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\initializer_list:89:5: note: candidate: 'template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)'
   89 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
c:\mingw\include\c++.1.0\initializer_list:89:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:32: note:   mismatched types 'std::initializer_list<_Tp>' and 'long double*'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:48:5: note: candidate: 'template<class _Container> decltype (__cont.begin()) std::begin(_Container&)'
   48 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
c:\mingw\include\c++.1.0\bits\range_access.h:48:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:32: note:   variable-sized array type 'long double [nPoints]' is not a valid template argument
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:58:5: note: candidate: 'template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)'
   58 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
c:\mingw\include\c++.1.0\bits\range_access.h:58:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:32: note:   variable-sized array type 'long double [nPoints]' is not a valid template argument
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:87:5: note: candidate: 'template<class _Tp, long long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   87 |     begin(_Tp (&__arr)[_Nm])
      |     ^~~~~
c:\mingw\include\c++.1.0\bits\range_access.h:87:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:32: note:   variable-sized array type 'long long int' is not a valid template argument
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:104:31: note: candidate: 'template<class _Tp> _Tp* std::begin(std::valarray<_Tp>&)'
  104 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&);
      |                               ^~~~~
c:\mingw\include\c++.1.0\bits\range_access.h:104:31: note:   template argument deduction/substitution failed:
testMain.cpp:20:32: note:   mismatched types 'std::valarray<_Tp>' and 'long double [nPoints]'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:105:37: note: candidate: 'template<class _Tp> const _Tp* std::begin(const std::valarray<_Tp>&)'
  105 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
      |                                     ^~~~~
c:\mingw\include\c++.1.0\bits\range_access.h:105:37: note:   template argument deduction/substitution failed:
testMain.cpp:20:32: note:   mismatched types 'const std::valarray<_Tp>' and 'long double [nPoints]'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                ^
testMain.cpp:20:41: error: no matching function for call to 'end(long double [nPoints])'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                         ^
In file included from c:\mingw\include\c++.1.0\bits\range_access.h:36,
                 from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\initializer_list:99:5: note: candidate: 'template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
c:\mingw\include\c++.1.0\initializer_list:99:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:41: note:   mismatched types 'std::initializer_list<_Tp>' and 'long double*'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                         ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:68:5: note: candidate: 'template<class _Container> decltype (__cont.end()) std::end(_Container&)'
   68 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
c:\mingw\include\c++.1.0\bits\range_access.h:68:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:41: note:   variable-sized array type 'long double [nPoints]' is not a valid template argument
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                         ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:78:5: note: candidate: 'template<class _Container> decltype (__cont.end()) std::end(const _Container&)'
   78 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
c:\mingw\include\c++.1.0\bits\range_access.h:78:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:41: note:   variable-sized array type 'long double [nPoints]' is not a valid template argument
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                         ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:97:5: note: candidate: 'template<class _Tp, long long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
   97 |     end(_Tp (&__arr)[_Nm])
      |     ^~~
c:\mingw\include\c++.1.0\bits\range_access.h:97:5: note:   template argument deduction/substitution failed:
testMain.cpp:20:41: note:   variable-sized array type 'long long int' is not a valid template argument
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                         ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:106:31: note: candidate: 'template<class _Tp> _Tp* std::end(std::valarray<_Tp>&)'
  106 |   template<typename _Tp> _Tp* end(valarray<_Tp>&);
      |                               ^~~
c:\mingw\include\c++.1.0\bits\range_access.h:106:31: note:   template argument deduction/substitution failed:
testMain.cpp:20:41: note:   mismatched types 'std::valarray<_Tp>' and 'long double [nPoints]'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));
      |                                         ^
In file included from c:\mingw\include\c++.1.0\string:54,
                 from c:\mingw\include\c++.1.0\bits\locale_classes.h:40,
                 from c:\mingw\include\c++.1.0\bits\ios_base.h:41,
                 from c:\mingw\include\c++.1.0\ios:42,
                 from c:\mingw\include\c++.1.0\ostream:38,
                 from c:\mingw\include\c++.1.0\iostream:39,
                 from testMain.cpp:1:
c:\mingw\include\c++.1.0\bits\range_access.h:107:37: note: candidate: 'template<class _Tp> const _Tp* std::end(const std::valarray<_Tp>&)'
  107 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
      |                                     ^~~
c:\mingw\include\c++.1.0\bits\range_access.h:107:37: note:   template argument deduction/substitution failed:
testMain.cpp:20:41: note:   mismatched types 'const std::valarray<_Tp>' and 'long double [nPoints]'
   20 |  YY.insert(YY.begin(), begin(yy), end(yy));

问题 1

您缺少声明 std::begin 的头文件。

添加

#include <iterator>

到你的文件。

问题 2

long double yy[nPoints];

不是标准的 C++。它作为扩展被一些编译器支持。

std::begin 不适用于 VLA。它仅适用于标准数组——其大小在编译时已知的数组。


鉴于您可以对 运行 时已知大小的数组使用 std::vector,因此无需使用 VLA。

而不是

long double yy[nPoints];

使用

std::vector<long double> yy(nPoints);