为未定义类型键入cast

Nid*_* MS 3 c++ casting class forward-declaration undefined-symbol

如何为前向声明的类实现类型转换操作符.我的代码是.

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;
};
Run Code Online (Sandbox Code Playgroud)

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

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

Fyt*_*tch 7

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

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);
}
Run Code Online (Sandbox Code Playgroud)