到目前为止,除此操作员外,其他所有操作员都可以正常工作。运行代码时,出现错误:“错误:后缀'Complex Complex :: operator ++(Complex)'必须采用'int'作为其参数|“
这里是我的代码:
#include<iostream>
#include<iomanip>
using namespace std;
class Complex
{
friend istream &operator>>(istream&, Complex&);
friend ostream &operator<<(ostream&, Complex&);
public:
Complex(float = 0, float = 0);
Complex operator+ (Complex);
Complex operator- (Complex);
Complex operator* (Complex);
问题出现在这里,告诉我参数应该是整数,但是我试图像以前的运算符一样传递Class实例化。
Complex operator++ (Complex);
private:
float real, imag;
};
Complex::Complex(float a, float b)
{
real = a;
imag = b;
}
Complex Complex::operator+(Complex one)
{
Complex two;
two.real = real + one.real;
two.imag = imag + one.imag;
return(two);
}
Complex Complex::operator-(Complex one)
{
Complex two;
two.real = real - one.real;
two.imag = imag - one.imag;
return(two);
}
Complex Complex::operator*(Complex one)
{
Complex two;
two.real = (real * one.real) + (imag * one.imag * (-1));
two.imag = (real * one.imag) + (one.real * imag);
return(two);
}
Complex Complex::operator++(Complex one)
{
Complex two;
two.real = (real * real * real) + (3 * real) * (imag * imag * (-1));
two.imag = 3 * (real * real)*imag + (imag * imag *imag * (-1));
return(two);
}
//Extraction Operator
istream &operator>> (istream &input, Complex &one)
{
input >> one.real >> one.imag;
}
//Insertion Operator
ostream &operator<<(ostream &output, Complex &one)
{
output << one.real <<"+"<< one.imag <<"i" << endl;
return output;
}
//Write stream insertion and extraction operators
int main()
{
Complex c1,c2,c3,sum,diff,prod;
cout << "Enter first complex number: ";
cin >> c1;
cout <<"Enter second complex number: ";
cin >> c2;
cout << "The first complex number is: " <<c1;
cout <<"The second complex number is: " <<c2;
sum = c1 + c2;
cout<<"The sum is: " <<sum;
diff = c1 - c2;
cout<<"The difference is: " <<diff;
prod = c1*c2;
cout<<"The product is: " <<prod;
if (c1==c2)
cout <<"Equal";
if (c1!=c2)
cout <<"Not equal";
//Cube function is the ++ operator
cout << "Preincrement: " <<++c1<<++c2;
cout << "Postincrement: " <<c1++<<c2++;
cout << "After post increment: "<<c1<<c2;
*/
return 0;
}
答案 0 :(得分:2)
您需要使用void atomicLeftShift(atomic<int>& var, int shiftBy)
{
While(true) {
int oldVal = var;
int newVal = oldVal << shiftBy;
if(var.compare_exchange_weak(oldVal, newVal));
break;
else
_mm_pause();
}
}
或(int)
作为()
的后缀。这将告诉编译器您是否希望operator++
运算符为前置或后缀。即++
或int++
。
这只是一种特质。
++int