C++编译器错误:传递指针,但编译器看到引用
C++ compiler error: passing pointers but compiler sees references
下面是类中的方法签名。
virtual void evaluate(const double *var, double *obj, double *constr) const = 0;
virtual void evaluate(unsigned int numPoints, const double **var, double **obj, double **constr) const {
//do something
}
这是参数的声明
unsigned int size;
double **var = new double*[size];
double **obj = new double*[size];
double **constr = new double*[size];
下面是方法调用。
evaluator.evaluate(size, var, obj, constr);
我收到以下编译器错误。
foo.cpp: In member function âvoid foo::evaluatePopulation(std::vector<Individual, std::allocator<Individual> >&, unsigned int, bool)â:
foo.cpp:347: error: no matching function for call to foo::evaluate(unsigned int&, double**&, double**&, double**&) constâ
foo.h:35: note: candidates are: virtual void foo::evaluate(const double*, double*, double*) const
foo.h:43: note: virtual void foo::evaluate(unsigned int, const double**, double**, double**) const <near match>
foo
是类名。我正在使用双指针(两个星号)。如何解决此错误?
在第二个签名中,第二个形式参数 var
的类型为 const double**
。constr
,实际的参数是 double**
类型的 hovewer,它不能隐式转换为前一种类型。
例
#include <stdio.h>
void fn(const int** pp)
{
printf("%p : %p : %d", pp, *pp, **pp);
}
int main()
{
int n = 1;
int *p = &n;
fn(&p); // ERROR. see below
return 0;
}
报告的错误是准确的:
main.c:17:8:将
'int **'
传递给类型为'const int **'
的参数会丢弃嵌套指针类型中的限定符。
相关文章:
- decltype(1, t) 应该是 l 值引用吗?(编译器不同意)
- C++编译器通过引用传递而没有我告诉它?
- 如何在构造函数中传递 const 引用时强制编译器不接受右值
- 现代编译器会优化只引用对象子集的局部变量吗
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- 对从引用到 std::reference_wrapper 或编译器错误的隐式转换的理解不正确?
- C++编译器,强制通过引用传递
- TBB 编译器错误 -"my_task":必须初始化引用
- C++错误:C2280 - 引用已删除函数的编译器错误?
- 提示编译器在使用 auto 时通过引用返回,而不使用 ->
- C++编译器如何在引用参数中实际传递文本常量?
- 为什么当类具有引用成员时C++编译器不删除复制构造函数
- 是否有任何C++编译器可以发出悬而未决的引用警告?
- 为什么在取消引用的指向接口的指针上使用赋值运算符不是编译器错误
- 编译器给出有关对函数的未定义引用的错误
- 三元运算符:编译器不发出局部变量警告的返回引用
- mingw-w64 中的编译器错误:"未定义对'__getreent'的引用。
- 为什么 C++ 编译器不警告返回对局部变量的引用
- 常量右值引用是否允许对编译器进行额外优化?
- 由于为 c++ 程序调用 ulldiv.asm 而导致编译器引用错误