Home > Mobile >  problem of using const when passing a pointer
problem of using const when passing a pointer

Time:02-16

Visual studio shows a error saying "the object has type quantifiers that are not compatible with the member function 'somfunc' "

class T_ship {
    public:    
        ... 
        float ship_run(int ship_len);
        // function ship_run doesn't change any class member values
};

There is a function in main() with *T_ship pointer passed as input.

void draw_ship(const T_ship * a_shp){

    float dist = a_ship-> ship_run(100); 
    // there is a red wavy line under a_ship     
}

Many thanks if someone can help.

CodePudding user response:

If you want the actual pointer to be const and not the object pointed to, put the const in front of the type, so like this:

T_ship * const

However in your case if the function ship_run doesn't modify anything you should mark it as const at the end of the function as well so like this:

float ship_run(int v) const { /* your code here */ }

  • Related