Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type cast for undefined type

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'

like image 477
Nidhin MS Avatar asked Sep 01 '25 03:09

Nidhin MS


1 Answers

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);
}
like image 176
Fytch Avatar answered Sep 02 '25 17:09

Fytch