How can I implement typecast operator for a forward declared class. My code is.
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;
};
When I compiled it I got an error
error C2027: use of undefined type 'CDB'
Just declare the conversion operators. Define them afterwards (after you completely defined the classes). E.G.:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With