C++ 没有用于调用“ ”的匹配函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15113856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no matching function for call to ' '
提问by Teodora
I was given to implement the function:
我被赋予实现该功能:
"static double distanta (const Complex&, const Complex&);"
which return the distance between two Complex numbers. The definition of the function it is inside the Complex class and I have implemented it like that:
返回两个复数之间的距离。函数的定义在 Complex 类中,我是这样实现的:
double Complex::distanta(const Complex &a, const Complex &b)
{
double x = a.real() - b.real();
double y = a.imag() - b.imag();
return sqrt(x * x + y * y);
}
As far as I know a static function can only access static members and my class only has
据我所知,静态函数只能访问静态成员,而我的类只有
double _re;
double _im;
as data members.
作为数据成员。
Within the main function I have called it as:
在主函数中,我将其称为:
#include <iostream>
#include "complex.h"
using namespace std;
int main()
{
Complex* firstComplexNumber;
firstComplexNumber = new Complex(81, 93);
cout << "Numarul complex este: " << *firstComplexNumber << endl;
Complex* secondComplexNumber;
secondComplexNumber = new Complex(31, 19);
cout << "Distanta dintre cele doua numere" <<endl << endl;
Complex::distanta(firstComplexNumber, secondComplexNumber);
return 0;
}
and the error I get is:
我得到的错误是:
error: no matching function for call to 'Complex::distanta(Complex*&, Complex*&)'
错误:没有匹配的函数调用'Complex::distanta(Complex*&, Complex*&)'
Could you please tell me what is it that I'm doing wrong? Thanks!
你能告诉我我做错了什么吗?谢谢!
回答by Joseph Mansfield
You are passing pointers (Complex*
) when your function takes references (const Complex&
). A reference and a pointer are entirely different things. When a function expects a reference argument, you need to pass it the object directly. The reference only means that the object is not copied.
Complex*
当您的函数接受引用 ( const Complex&
)时,您正在传递指针( )。引用和指针是完全不同的东西。当函数需要引用参数时,您需要直接将对象传递给它。引用仅表示不复制对象。
To get an object to pass to your function, you would need to dereference your pointers:
要让对象传递给您的函数,您需要取消引用您的指针:
Complex::distanta(*firstComplexNumber, *secondComplexNumber);
Or get your function to take pointer arguments.
或者让你的函数接受指针参数。
However, I wouldn't really suggest either of the above solutions. Since you don't need dynamic allocation here (and you are leaking memory because you don't delete
what you have new
ed), you're better off not using pointers in the first place:
但是,我不会真正建议上述任何一种解决方案。由于您在这里不需要动态分配(并且您正在泄漏内存,因为您没有delete
您所new
编辑的内容),因此您最好不要首先使用指针:
Complex firstComplexNumber(81, 93);
Complex secondComplexNumber(31, 19);
Complex::distanta(firstComplexNumber, secondComplexNumber);
回答by Andy Prowl
You are trying to pass pointers (which you do not delete, thus leaking memory) where references are needed. You do not really need pointers here:
您正试图在需要引用的地方传递指针(您不会删除,因此会泄漏内存)。你真的不需要这里的指针:
Complex firstComplexNumber(81, 93);
Complex secondComplexNumber(31, 19);
cout << "Numarul complex este: " << firstComplexNumber << endl;
// ^^^^^^^^^^^^^^^^^^ No need to dereference now
// ...
Complex::distanta(firstComplexNumber, secondComplexNumber);