加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 未定义类型的类型转换

发布时间:2020-12-16 09:53:55 所属栏目:百科 来源:网络整理
导读:如何为前向声明的类实现类型转换操作符. 我的代码是. class CDB;class CDM{public: CDM(int = 0,int = 0); operator CDB() const //error { }private: int m_nMeters; int m_nCentimeters;};class CDB{public: CDB(int = 0,int = 0); operator CDM() const /
如何为前向声明的类实现类型转换操作符.
我的代码是.

class CDB;
class CDM
{
public:
    CDM(int = 0,int = 0);
    operator CDB() const  //error
    {
    }
private:
    int     m_nMeters;
    int     m_nCentimeters;
};

class CDB
{
public:
    CDB(int = 0,int = 0);
    operator CDM() const  //error
    {
    }
private:
    int     m_nFeet;
    int     m_nInches;
};

当我编译它时,我得到一个错误

错误C2027:使用未定义类型’CDB’

解决方法

只需声明转换运算符.之后定义它们(在完全定义类之后).例如.:

class CDB;
class CDM
{
public:
    CDM(int = 0,int = 0);
    operator CDB() const; // just a declaration
private:
    int     m_nMeters;
    int     m_nCentimeters;
};

class CDB
{
public:
    CDB(int = 0,int = 0);
    operator CDM() const // we're able to define it here already since CDM is already defined completely
    {
        return CDM(5,5);
    }
private:
    int     m_nFeet;
    int     m_nInches;
};
CDM::operator CDB() const // the definition
{
    return CDB(5,5);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读