错误:返回类型 'class Polar' 不完整,无效使用类型 'polar'
Error: return type 'class Polar' is incomplete, invalid use of type 'polar'
我想使用转换运算符将 Rect 类转换为 Polar 类,但是我收到错误,指出"不完整的类型"。我没有得到任何东西使用指针而不是对象本身时出错。但我不能回来指向用于强制转换的对象的指针。
#include<iostream>
#include<cmath>
using namespace std;
class Polar;
class Rect{
double x;
double y;
public:
Rect(double xx, double yy): x(xx), y(yy) {}
void display(void){
cout<<x<<endl;
cout<<y<<endl;
}
operator Polar(){//need help regarding this function
return Polar(sqrt(x*x + y*y) , atan(y/x)*(180/3.141));
}
};
class Polar{
double r;
double a;
double x;
double y;
public:
Polar(double rr, double aa){
r=rr;
a=aa*(3.141/180);
x= r* cos(a);
y= r* sin(a);
}
Polar(){
r=0;
a=0;
x=0;
y=0;
}
Polar operator+(Polar right){
Polar temp;
//addition
temp.x= x+ right.x;
temp.y= x+ right.y;
//conversion
temp.r= sqrt(temp.x*temp.x + temp.y*temp.y);
temp.a= atan(temp.y/temp.x)*(180/3.141);
return temp;
}
operator Rect(){
return Rect(x,y);
}
friend ostream & operator <<(ostream &out, Polar a);
double getr(){
return r;
}
double geta(){
return a;
}
};
ostream & operator <<(ostream &out,Polar a){
out<<a.getr()<<", "<<a.geta()<<endl;
return out;
}
int main()
{
Polar a(10.0, 45.0);
Polar b(8.0, 45.0);
Polar result;
//+ operator overloading
result= a+b;
//<< overloading
cout<<result<<endl;
Rect r(18,45);
Polar p(0.2,53);
r=p;//polar to rect conversion
r.display();
return 0;
}
有没有办法我可以在 Rect 类中使用 Polar 类的对象。如果那么指针如何用于铸造目的。
不,您不能使用任何依赖于Rect
内部Polar
定义的内容。 Polar
尚未定义。相反,将operator Polar() { ... }
更改为声明:operator Polar();
并将其定义放在Polar
之后:
inline Rect::operator Polar() {
return Polar(sqrt(x*x + y*y) , atan(y/x)*(180/3.141));
}
顺便说一下,这个运算符是一个转换运算符。演员阵容是要求转换的一种方式,但不是唯一的方法。
哦,还有,operator Polar()
应该是const
的,因为它不会修改它所应用到的对象。所以operator Polar() const;
在Rect
的定义中,inline Rect::operator Polar() const { ... }
定义。
相关文章:
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 无法访问嵌套类.类的使用无效
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- FFMPEG配置文件级别id大小无效
- 错误:从"int"到枚举c++的转换无效
- 如何修复此错误:className::className的无效使用
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- C++-模板嵌套类的引用初始化无效
- 错误:无效的预处理指令 #i 的意思是 #if?
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 从 'int' 到 'int*' CPP 的转换无效
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- C++ PTHREADS - 无效转换无效*(*)()到无效*(*)(无效*)
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 数组下标的类型"float*[float]"无效
- 错误:返回类型 'class Polar' 不完整,无效使用类型 'polar'