错误:调用'begin(long double [nPoints])'没有匹配函数;使用硬编码的 int 与整数变量初始化向量

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

本文关键字:编码 函数 整数 初始化 向量 变量 int long begin 调用 double      更新时间:2023-10-16

我写了一个处理幻数的代码,现在我正在尝试将幻数放入变量中。 本质上,我创建一个具有固定数量的元素的数组,填充数组,然后将其转换为向量。 只要我将数组创建为:

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;
}

第一个返回以下错误消息:

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:mingwincludec++9.1.0bitsrange_access.h:36,
                 from c:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0initializer_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:mingwincludec++9.1.0initializer_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_access.h:48:5: note: candidate: 'template<class _Container> decltype (__cont.begin()) std::begin(_Container&)'
   48 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_access.h:104:31: note: candidate: 'template<class _Tp> _Tp* std::begin(std::valarray<_Tp>&)'
  104 |   template<typename _Tp> _Tp* begin(valarray<_Tp>&);
      |                               ^~~~~
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_access.h:36,
                 from c:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0initializer_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:mingwincludec++9.1.0initializer_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_access.h:68:5: note: candidate: 'template<class _Container> decltype (__cont.end()) std::end(_Container&)'
   68 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_access.h:106:31: note: candidate: 'template<class _Tp> _Tp* std::end(std::valarray<_Tp>&)'
  106 |   template<typename _Tp> _Tp* end(valarray<_Tp>&);
      |                               ^~~
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0string:54,
                 from c:mingwincludec++9.1.0bitslocale_classes.h:40,
                 from c:mingwincludec++9.1.0bitsios_base.h:41,
                 from c:mingwincludec++9.1.0ios:42,
                 from c:mingwincludec++9.1.0ostream:38,
                 from c:mingwincludec++9.1.0iostream:39,
                 from testMain.cpp:1:
c:mingwincludec++9.1.0bitsrange_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:mingwincludec++9.1.0bitsrange_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);