c++visual-studiodeclaration

Why does MSVC think that the input parameter to this function is of type int, although it's not?


I tried to learn about the implicit this pointer and its uses, but I can't seem to understand why my compiler (MSVC) keeps talking about int as the type for the function printcmon(), instead of cmon.

void printcmon(const cmon* e);

class cmon
{
public:
    int x, y;
    cmon(int x, int y) {
            this->x = x;
            this->y = y;
            printcmon(this);
    }

    int getX() const {
        const cmon* e = this;
        std::cout << e->x << std::endl;
        return 0;
    }
};

void printcmon(const cmon* e) {
    //stuff
}

Errors:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error: missing ',' before '*'
error C2664: 'void printcmon(const int)': cannot convert argument 1 from 'cmon *' to 'const int'

message : There is no context in which this conversion is possible
message : see declaration of 'printcmon'
message : while trying to match the argument list '(cmon *)'

Solution

  • There is no class cmon before first line void printcmon(const cmon* e); Add a declaration:

    class cmon;
    void printcmon(const cmon* e);
    
    class cmon
    {
    public:
        int x, y;
        cmon(int x, int y) {
            this->x = x;
            this->y = y;
            printcmon(this);
        }
    
        int getX() const {
            const cmon* e = this;
            std::cout << e->x << std::endl;
            return 0;
        }
    };
    
    void printcmon(const cmon* e) {
        //stuff
        
    }