Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

problem of using const when passing a pointer

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 */ }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading