从一个班级到另一个类的打字
从si
转换为Imperialsystem
正在起作用,但反向不起作用。 错误消息: static_cast:无法从帝国系统转换为si
代码:
#include<iostream>
#define endl '\n'
using std::cout;
#define MTRTOFEETRATIO 3.28084;
/*
Write two classes to store distances in meter-centimeter and feet-inch systems respectively. Write conversions functions so that the program can convert
objects of both types.
*/
class SI;
class ImperialSystem {
private:
int mfeet;
int minch;
public:
ImperialSystem(int m, int cm) :mfeet{ m }, minch{ cm }{};
ImperialSystem(float dis) :mfeet{ static_cast<int>(dis) }, minch{ static_cast<int>((dis - mfeet) * 12) } {}
operator float() {
return mfeet + minch / 12.0;
}
operator SI();
friend std::ostream& operator <<(std::ostream& out, const ImperialSystem& dis);
};
class SI {
private:
int mmeter;
int mcentimeter;
public:
SI(int m, int cm) :mmeter{ m }, mcentimeter{ cm }{};
SI(float dis) :mmeter{ static_cast<int>(dis) }, mcentimeter{ static_cast<int>((dis - mmeter) * 12) } {}
operator ImperialSystem();
friend std::ostream& operator <<(std::ostream& out, const SI& dis);
};
std::ostream& operator <<(std::ostream& out, const SI& dis) {
out << " " << dis.mmeter << " m " << dis.mcentimeter << " cm ";
return out;
}
std::ostream& operator <<(std::ostream& out, const ImperialSystem& dis) {
out << " " << dis.mfeet << " ft " << dis.minch << " in ";
return out;
}
ImperialSystem::operator SI() {
double feet = mfeet + minch / 12;
double meter = feet / MTRTOFEETRATIO;
return meter;
}
SI::operator ImperialSystem() {
double meter = mmeter + mcentimeter / 100.0;
double feet = meter * MTRTOFEETRATIO;
return feet;
}
int main() {
SI s{ 20,35 };
cout << s << " = " << static_cast<ImperialSystem>(s) << endl;//this works
ImperialSystem i{ 10,11 };
cout << i << " = " << static_cast<SI>(i) << endl;//but this doesnot
return 0;
}
Conversion from SI
to ImperialSystem
is working but the reverse isnot working.
The error message:static_cast: cannot convert from ImperialSystem to SI
code:
#include<iostream>
#define endl '\n'
using std::cout;
#define MTRTOFEETRATIO 3.28084;
/*
Write two classes to store distances in meter-centimeter and feet-inch systems respectively. Write conversions functions so that the program can convert
objects of both types.
*/
class SI;
class ImperialSystem {
private:
int mfeet;
int minch;
public:
ImperialSystem(int m, int cm) :mfeet{ m }, minch{ cm }{};
ImperialSystem(float dis) :mfeet{ static_cast<int>(dis) }, minch{ static_cast<int>((dis - mfeet) * 12) } {}
operator float() {
return mfeet + minch / 12.0;
}
operator SI();
friend std::ostream& operator <<(std::ostream& out, const ImperialSystem& dis);
};
class SI {
private:
int mmeter;
int mcentimeter;
public:
SI(int m, int cm) :mmeter{ m }, mcentimeter{ cm }{};
SI(float dis) :mmeter{ static_cast<int>(dis) }, mcentimeter{ static_cast<int>((dis - mmeter) * 12) } {}
operator ImperialSystem();
friend std::ostream& operator <<(std::ostream& out, const SI& dis);
};
std::ostream& operator <<(std::ostream& out, const SI& dis) {
out << " " << dis.mmeter << " m " << dis.mcentimeter << " cm ";
return out;
}
std::ostream& operator <<(std::ostream& out, const ImperialSystem& dis) {
out << " " << dis.mfeet << " ft " << dis.minch << " in ";
return out;
}
ImperialSystem::operator SI() {
double feet = mfeet + minch / 12;
double meter = feet / MTRTOFEETRATIO;
return meter;
}
SI::operator ImperialSystem() {
double meter = mmeter + mcentimeter / 100.0;
double feet = meter * MTRTOFEETRATIO;
return feet;
}
int main() {
SI s{ 20,35 };
cout << s << " = " << static_cast<ImperialSystem>(s) << endl;//this works
ImperialSystem i{ 10,11 };
cout << i << " = " << static_cast<SI>(i) << endl;//but this doesnot
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
消除,
来自您的SI课程。
这会在MSV和Clang编译器中造成构造和铸造的困惑。
Remove,
from your SI class.
This creates confusion in construction and casting in msvs and clang compiler.