Xcode [8.0]
C++
Code:
#include< iostream>
#include< math.h>
#include”MyComplex.h”
usingnamespacestd;
int main(int argc,constchar * argv[]) {
//insert code here…
MyComplex c1;
MyComplex c2.
Cin & gt; > c1;
C2 = c1;
Cout & lt; < c1 < < endl;
Cout & lt; < c2 < < endl;
MyComplex c3.
C3 = c1 + c2; //error1:No viable overloaded ‘=’
Cout & lt; < c1+c2 < < Lendl; //error2: No matching constructor for initialization of ‘MyComplex’
Cout & lt; < c1-c2 < < Lendl; //error
Cout & lt; < c1/c2 < < Lendl; //error
Cout & lt; < c1*c2 < < Lendl; //error
Return0;
}
So how is the overloading of = defined?Look at the code.
MyComplex& operator = (MyComplex & rhs){
Real = RHS. GetReal ();
Imaginary = RHS. GetImaginary ();
Return * this;
}
It doesn’t seem to be a problem. By looking at http://www.cplusplus.com/forum/general/153830/ to find the reason of the error:
A temporary object can’t bind to A non-const reference.
(A temporary object cannot be bound to an extraordinary reference, that is, the bound reference must be constant)
Therefore, the parameter of the overloaded = function must be const. (Such a covert mistake is more than enough to cry about…)
Error1 disappears after adding const:
MyComplex& operator = (constMyComplex & rhs){
Real = RHS. GetReal ();
Imaginary = RHS. GetImaginary ();
Return * this;
}
Error2 has the same error cause.
Friend ostream& operator< < (ostream& os,const MyComplex & c);
Const is very important! Want to! !!! After the correction, the errors disappeared
Read More:
- C# Compiler Errors
- Three kinds of errors in C + + program
- Oracle 12C installation process related errors and Solutions
- C + + common errors: “error: XXX in namespace ‘STD’ does not name a template type”
- Solution pandas.errors.ParserError : Error tokenizing data. C error: Buffer overflow caught
- Python error: pandas.errors.ParserError: Error tokenizing data. C error: Expected 3……
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- error C4996: ‘scanf‘: This function or variable may be unsafe.Visual Studio Series compilers report errors using scanf function
- C++:error C2228: left of ‘.str’ must have class/struct/union
- Differences between length() size() and C strlen() of C + + string member functions
- C / C + + rounding function ceil(), floor ()
- There are errors 1304 and 2350 FDI server errors in ArcGIS Server installation
- Solution to errors reported by TES command in vscode – errors reported by typescript command
- Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +
- Error C2664 in C + + compilation: cannot convert parameter 2 from “const char [5]” to “lpctstr” solution.
- Error c2137 of C language: empty character constant (Fixed)
- Solution to the problem of no C / C + + option in the new project of eclipse
- C / C + + error handling (document): unhandled exception: 0xc0000005: access violation while reading location 0x00000000
- Solution to the flash of running result of dev C / C + +
- C / C + + library function (tower / tower) realizes the conversion of letter case