模板和 for 循环出现问题
Trouble with templates and for loops
我有这段代码,它给了我一个指示两个问题的单一错误。
int healthyConst = 0;
int sickConst = 1;
int recoveredConst = 2;
GraphMatrix<int, double> graph (100);
for (int i = 0; i < sampleSize; i++)
{
if(std::rand() % 2 > 0.05) graph.setVertexInfo(i, sickConst); //Error
else graph.setVertexInfo(i, healthyConst);
}
错误是:
error: 调用 GraphMatrix::setVertexInfo(int&, int*) 时没有匹配函数
有问题的函数在源代码中声明如下:
void GraphMatrix::setVertexInfo(int v, VertexObject& info)
首先,我不应该成为参考。这对我来说似乎是荒谬的,但我无法解决这个问题。如果我试图智胜编译器并键入for(int* i = 0...)
错误现在抱怨setVertexInfo(int&*, int*)
,我什至不明白这意味着什么。
其次,病康斯特不是一个指针。它只是一个整数。现在我意识到该方法,如所写,接受VertexObject&,而不是VertexObject,但也*sickConst
导致编译器抱怨invalid type argument of 'unary *'
。我也尝试了&sickConst
,编译器并不意外地将其解释为指针。
另请注意,对于 for 循环的第二行,可能出于相同的原因抛出相同的错误。
问题是:为什么我会收到这些错误,我该如何修复它们?
您声明源代码中的函数声明如下:
void GraphMatrix::setVertexInfo(int v, VertexObject& info)
但是,在您的 for 循环中,您正在传递一种int
。要么更改函数声明和定义以接受某种类型的int
,要么更改作为VertexObject
传递给函数的类型。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在java中解决这段代码时面临循环中的问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 退出简单while循环时出现问题
- 循环无限运行C++解决骑士之旅问题
- Visual Studio 2017循环自动向量化问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 嵌套While循环不起作用(C++问题)
- 使用 GMP 的 while 循环出现问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- OpenMP for 循环并行性问题
- 这个返回元素位置的基于循环的函数有什么问题?
- 为什么循环会导致指针出现问题?
- 使用基于范围的循环迭代矢量时出现问题
- 在 c++ 中循环遍历数组时遇到问题
- 运行无限循环的最小二叉树问题
- 在C++中循环访问自定义结构列表的小问题
- 在 for 循环中更新两个变量时遇到问题C++