错误:调用'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
我写了一个处理幻数的代码,现在我正在尝试将幻数放入变量中。 本质上,我创建一个具有固定数量的元素的数组,填充数组,然后将其转换为向量。 只要我将数组创建为:
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);
相关文章:
- 为二进制树编码remove()函数
- 如何将 MATLAB 图像处理库内置函数转换为 MATLAB 编码器代码生成不支持的 C++?
- 如何将 50 行和 50 列的二维数组编码为一个为元素随机分配星号的函数?
- 正确编码和调试的命名空间中未解析的函数
- 如何编码为像 STL IO 操纵器函数这样的样式?
- 错误:调用'begin(long double [nPoints])'没有匹配函数;使用硬编码的 int 与整数变量初始化向量
- 如何正确编码此函数,以便它计算 int 的每个数字的总和
- Simulink-使用.Mat作为自动编码c函数的外部输入变量
- 干净编码:如何从循环中的函数调用内部中断循环
- 为什么 unicode 编码需要函数 wsetlocale()
- 使用数组函数和循环求解伪元编码|C
- 如何在 C# 中对此C++函数进行编码
- 为什么我的编码出现错误无法在函数 main 中将 int 转换为 int *
- 了解 Matlab 中的 DEL2 函数,以便用 C++ 进行编码
- 编码器在Matlab上实现C++、sym和perms函数
- 为什么C/C++编码标准说我们不应该在函数中有多个返回
- 我可以让函数调用软编码吗
- 多个相似的函数编码样式
- 使用 swap 作为函数而不是在算法本身内更快地编码
- C++中Base64编码函数的逐位移位