c – 未定义类型的类型转换
作者:互联网
如何为前向声明的类实现类型转换操作符.
我的代码是.
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);
}
标签:c,class,casting,forward-declaration,undefined-symbol 来源: https://codeday.me/bug/20190728/1561461.html