从一个班级到另一个类的打字

发布于 2025-02-13 18:45:59 字数 2256 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-02-20 18:45:59

消除,

操作员float(){
返回MFEET + Minch / 12.0;
}

来自您的SI课程。

这会在MSV和Clang编译器中造成构造和铸造的困惑。

Remove,

operator float() {
return mfeet + minch / 12.0;
}

from your SI class.

This creates confusion in construction and casting in msvs and clang compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文