3D 矢量 - "No instance of overload function?"
3D Vector - "No instance of overload function?"
C 中的向量仍然相对较新,此函数的目的是进行4个参数,其中3个定义了正在编写的数据的(x,y,z(位置,并且第四是要编写的价值。
根据要求,列出了错误的图片:
上面列出的代码的图片
问题在" push_back"代码下。这 "。"yy.push and xx.push给出错误"没有超载函数的实例"。
如果有人可以解释这意味着什么以及如何解决它,我将非常感谢它!:(
double datawrite(vector<unsigned int> xx, vector<unsigned int> yy,
vector<unsigned int> zz, double val) {
//Writes data to the 3d Vector
//finds coordinates for data
vector< vector< vector<unsigned int > > > xx;
vector< vector<unsigned int> > yy;
vector<unsigned int> zz;
//Writes value at proper position
zz.push_back(val);
yy.push_back(zz);
xx.push_back(yy);
//outputs value from vector
return val;
}
所以您想要一个双打的3D矩阵吗?首先,您需要创建它:
#include <vector>
std::vector<vector<vector<double>>> matrix;
这会创建一个3D矩阵,但具有0个大小。接下来,当您将数据添加到矩阵中时,您需要确保矩阵足够大:
// Co-ords are integers
double datawrite(int x, int y, int z, double val)
{
// Make sure vectors are large enough
if (matrix.size() < x+1) matrix.resize(x+1);
if (matrix[x].size() < y+1) matrix[x].resize(y+1);
if (matrix[x][y].size() < z+1) matrix[x][y].resize(z+1);
// Store the value
matrix[x][y][z] = val;
return val;
}
但是,这有点混乱,并且矩阵处于不完整状态。例如,如果您调用datawrite(2, 3, 4, 9.9);
,则可能会显示所有索引&lt;2,3,4是有效的,但事实并非如此。例如,尝试阅读matrix[0][0][0]
会给您带来错误。
您可以使用dataread
函数来解决此问题,该功能在尝试从它们中读取之前检查向量的大小。
如果您提前知道矩阵有多大,则可以立即创建整个矩阵:
vector<vector<vector<double>>> matrix(10, vector<vector<double>>(10, vector<double>(10)));
这将创建一个完整的10x10x10矩阵。这确保了所有索引&LT;10将有效。我更喜欢这种方法。然后您的功能变为:
double datawrite(int x, int y, int z, double val)
{
// Make sure indexes are valid
if (x >= matrix.size() || y >= matrix[x].size() || z >= matrix[x][y].size()) {
// Up to you what to do here.
// Throw an error or resize the matrix to fit the new data
}
// Store the value
matrix[x][y][z] = val;
return val;
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 传递给std::function template的template参数究竟代表什么
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 不断"Attempting to reference a deleted function"
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- C++ std::function 对于类 exept 的所有实例都是空的(只有 Visual2019 编译器问题)
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- std::function std::bind with lambda overload ambiguity
- 3D 矢量 - "No instance of overload function?"
- "No instance of overload function"