我尝试了以下代码,但出现错误。 在该重载赋值运算符中出现错误。 所以,请检查我的错误是: 我的运算符重载语法正确吗? 我希望解决特定的问题。
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class complex
{
int n,i;
int *x=new int[n];
int *y=new int[n];
public:
complex(int n)
{
cout<<"enter values of x:"<<endl;
for(i=0;i<n;i++)
cin>>*(x+i);
cout<<"values of x are:"<<endl;
for(i=0;i<n;i++)
cout<<*(x+i)<<endl;
}
complex operator =(complex *y)
{
cout<<"values of y are:"<<endl;
for(i=0;i<n;i++)
{
*(y+i)=*(x+i);
cout<<*(y+i)<<endl;
}
}
complex(complex &)
{
cout<<"copy constructor called:"<<endl;
}
~complex()
{
cout<<"destructorr called:"<<endl;
}
};
int main()
{
complex obj1(5);
complex obj2(obj1);
obj2=obj1;
return 0;
}
我得到的错误: 在成员函数'complex complex :: operator =(complex *) [错误]'operator <<'不匹配(操作数类型为'std :: ostream {aka std :: basic_ostream}'和'complex')
答案 0 :(得分:0)
您的代码中有很多错误。
其中大多数的原因是因为您拥有using namespace std;
编译器将自己的复杂库误认为其复杂库,所以
In member function 'complex complex::operator=(complex *) [Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'complex')
错误。永远不要使用using namespace std;
并在将来为您省掉很多麻烦!
对于副本分配和构造函数,您应该获取相同类型的const引用,并将所有变量相应地更改为另一个变量的精确副本!
也不要忘记复制分配的返回类型应作为参考!
由于这是您的第一个问题,因此我更正了所有错误并添加了一些有用的内容。只需关注并使用评论即可:
#include<bits/stdc++.h> // you don't need this for this example
#include<iostream>
//using namespace std; <-- never use this. it is very bad!
class complex
{
int n; // yu don't need i
int *x=new int[n];
int *y=new int[n];
public:
complex(int n_value):n(n_value) // <--first initialize n like this
{
std::cout<<"enter values of x:"<<std::endl;
for(int i=0;i<n;++i) // <-- try to use pre-increment instead
std::cin>>*(x+i);
std::cout<<"enter values of y:"<<std::endl; //<-- do not forget about the other dynamic array
for(int i=0;i<n;++i) // <-- try to use pre-increment instead
std::cin>>*(y+i);
std::cout<<"values of x and y are:"<<std::endl;
for(int i=0;i<n;++i){ // <-- try to use pre-increment instead
std::cout<<"x[" << i << "] is " << *(x+i)<<std::endl;
std::cout<<"y[" << i << "] is " << *(y+i)<<std::endl;
}
}
complex& /*here the return type should be reference of the type*/ operator =(const complex& another)// for this operator you should get another variable of this type as const reference
{
delete[] x; // <-- do NOT forget to delete the dynamic memory first
delete[] y; // <-- do NOT forget to delete the dynamic memory first
n = another.n; //reassign n from another
//call for another dynamic memory again
x=new int[n];
y=new int[n];
std::cout<<"values of y and x are:"<<std::endl;
for(int i=0;i<n;++i) // <-- try to use pre-increment instead
{
*(x+i) = *(another.x+i);
*(y+i) = *(another.y+i); // do not forget about the other dynamic array, the y
std::cout<<"x[" << i << "] is " << *(x+i)<<std::endl;
std::cout<<"y[" << i << "] is " << *(y+i)<<std::endl;
}
return *this; //
}
complex(const complex & another):n(another.n) // <--first initialize n like this // for this operator you should get another variable of this type as const reference
{
//here, try this one your self! Tip it is like assignment operator!
std::cout<<"copy constructor called:"<<std::endl;
}
~complex()
{
std::cout<<"destructorr called:"<<std::endl;
}
};
int main()
{
complex obj1(5);
complex obj2(obj1);
obj2=obj1;
return 0;
}