#include <iostream>
#include <string>
using namespace std;
namespace ComplexNumberNamespace
{
typedef struct _Data
{
double real;
double imag;
}Data;
class ComplexNumber
{
private:
Data data{};
public:
ComplexNumber();
ComplexNumber(float real, float imag);
ComplexNumber(ComplexNumber&);
string to_string();
void operator = (ComplexNumber&);
ComplexNumber operator + (ComplexNumber& rhs);
};
string ComplexNumber::to_string() {
std::string str;
str = std::to_string(data.real);
str.append(" + i ");
str.append(std::to_string(data.imag));
return str;
}
ComplexNumber::ComplexNumber(ComplexNumber & rhs) {
std::cout<<"copy constructor";
data.real = rhs.data.real;
data.imag = rhs.data.imag;
}
ComplexNumber::ComplexNumber() {
data.real = 0.0;
data.imag = 0.0;
}
void ComplexNumber::operator=(ComplexNumber &cplx) {
std::cout<<"assignment operator";
data.real = cplx.data.real;
data.imag = cplx.data.imag;
}
ComplexNumber::ComplexNumber(float real, float imag) {
data.real = real;
data.imag = imag;
}
ComplexNumber ComplexNumber::operator+(ComplexNumber &rhs)
{
ComplexNumber temp(data.real + rhs.data.real, data.imag+rhs.data.imag);
return temp;
}
}
using namespace ComplexNumberNamespace;
int main()
{
ComplexNumber c1(1, 2);
ComplexNumber c2(4, 3);
ComplexNumber sum = c1 + c2;
std::cout<<sum.to_string();
}
编译器输出
====================[ Build | all | Debug ]=====================================
"C:Program FilesJetBrainsCLion 2020.3.2bincmakewinbincmake.exe" --build C:UserspcsourcereposDoxygeninput_cpp_filescmake-build-debug --target all -- -j 3
Scanning dependencies of target input_cpp_files
[ 50%] Building CXX object CMakeFiles/input_cpp_files.dir/MyCppFile.cpp.obj
C:UserspcsourcereposDoxygeninput_cpp_filesMyCppFile.cpp: In function 'int main()':
C:UserspcsourcereposDoxygeninput_cpp_filesMyCppFile.cpp:79:28: error: cannot bind non-const lvalue reference of type 'ComplexNumberNamespace::ComplexNumber&' to an rvalue of type 'ComplexNumberNamespace::ComplexNumber'
ComplexNumber sum = c1 + c2;
~~~^~~~
C:UserspcsourcereposDoxygeninput_cpp_filesMyCppFile.cpp:40:5: note: initializing argument 1 of 'ComplexNumberNamespace::ComplexNumber::ComplexNumber(ComplexNumberNamespace::ComplexNumber&)'
ComplexNumber::ComplexNumber(ComplexNumber & rhs) {
^~~~~~~~~~~~~
mingw32-make.exe[2]: *** [CMakeFilesinput_cpp_files.dirbuild.make:83: CMakeFiles/input_cpp_files.dir/MyCppFile.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFilesMakefile2:95: CMakeFiles/input_cpp_files.dir/all] Error 2
mingw32-make.exe: *** [Makefile:103: all] Error 2
为什么我的源代码产生这个错误?
我做错了什么?
我该如何解决这个问题?
以ComplexNumber&
为参数的构造函数和操作符重载需要以const ComplexNumber&
为参数。
ComplexNumber sum = c1 + c2
这行实际上调用你的ComplexNumber复制构造函数,并给它c1 + c2
表达式的结果,这是一个临时的ComplexNumber。由于在c++中不能对临时对象进行非const引用,因此会得到此错误。
struct ComplexNumber {
// ...
// Copy
ComplexNumber(const ComplexNumber& other) { *this = other; }
ComplexNumber& operator=(const ComplexNumber& other) {
if (this != &other) { /* copy your attributes from other to this */ }
return *this;
}
// Move (optional - and not really useful in your case)
ComplexNumber(ComplexNumber&& other);
ComplexNumber& operator=(ComplexNumber&& other);
// Unlike operator= we return a temporary value,
// so it's a ComplexNumber indeed (no &)
// We'll modify neither 'this' nor 'rhs', so mark them const
ComplexNumber operator+(const ComplexNumber& rhs) const;
~ComplexNumber();
};
详细信息:https://en.cppreference.com/w/cpp/language/rule_of_three
一些技巧:
- 不要把
using namespace std
放在标题(我猜这是一个标题) typedef struct _Data { ... } Data;
是旧的C符号。你可以直接使用struct Type { ... } myObj;