no matching function for call to ' '

前端 未结 2 1494
旧时难觅i
旧时难觅i 2020-12-17 11:07

I was given to implement the function:

 \"static double distanta (const Complex&, const Complex&);\"

which return the distance betw

相关标签:
2条回答
  • 2020-12-17 11:34

    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);
    
    0 讨论(0)
  • 2020-12-17 11:35

    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.

    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 newed), you're better off not using pointers in the first place:

    Complex firstComplexNumber(81, 93);
    Complex secondComplexNumber(31, 19);
    Complex::distanta(firstComplexNumber, secondComplexNumber);
    
    0 讨论(0)
提交回复
热议问题