无法将"notes"从"std::array<double, 6u>"转换为"std::array<double, 7u>
Could not convert 'notes' from 'std::array<double, 6u>' to 'std::array<double, 7u>
我有一个与push_back()
相同的函数,但它不起作用。
我该如何解决这个问题?
#include <iostream>
#include <array>
#include <string>
using namespace std;
double add(array<double, 6> const& tab);
void add_push_back(array<double, 6+1> tab);
int main()
{
const int tailleTab{6};
array<double, tailleTab> notes = { 11.0, 9.5, 8.4, 12.0, 14.01, 12.03 };
double myMoyenne{};
myMoyenne = add(notes);
cout << myMoyenne;
add_push_back(notes);
for (auto note: notes){
cout << note << endl;
}
return 0;
}
double add(array<double, 6> const& tab){
double result{};
for (double note: tab){
result += note;
}
result /= tab.size();
return result;
}
void add_push_back(array<double, 6+1> tab){
array<double, 6+1> push;
for (unsigned int i = 0; i < tab.size(); ++i){
push.at(i) += tab.at(i);
}
push.at(7) = {7};
for (unsigned int i = 0; i < push.size(); ++i){
tab.at(i) += push.at(i);
}
}
错误:
error: could not convert 'notes' from 'std::array<double, 6u>' to 'std::array<double, 7u>'|
您不能将包含6个元素的std::array
传递到一个函数中,该函数期望std::array
包含7个元素,因为std::array<type, 6>
是与std::array<type, 7>
不同的类型。具有不同模板参数的模板类被编译器认为是不同的类型。
要解决眼前的问题,需要将add_push_back
的tab
参数从array<double, 6+1>
更改为array<double, 6>
。
但是,我建议您使用更适合调整大小的std::vector
或std::deque
。快速查看代码表明,您甚至无法对数组执行您想要执行的操作。您不能在运行时动态调整std::array
的大小。
您的数组大小不匹配。notes
数组有6个元素,而预期有7个元素。参见add_push_back
的签名,参数为array<double, 6+1>
。
相关文章:
- EASTL矢量<向量<int>>连续的
- C++在数学计算中将double转换为int
- 在c++中为double类型的数组创建一个unique_ptr
- vector<vector<double>> to mxArray using memcpy
- (double) 和 double() 之间的区别
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++ Version Of Double.longBitsToDouble
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 将 std::vector<double> 从 C++ 包装到 C 以在 Swift 中使用
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- 如何在<double>矢量中创建复杂类型的模板<T>?
- "double* grade"、"double *grade"和"double* fn()"有什么区别?
- 在不损失精度的情况下将double从C++传输到python
- 重复使用预分配的向量<复杂<double>>作为<double>长度两倍的向量
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- std::map<std::set, double> AND std:<long>map< std::p air<long, long>, double>