[英]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
,即,此類的hovewer double**
不能被隱式轉換為前一種類型。
例
#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 **'
會丟棄嵌套指針類型中的限定符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.