• Register
0 votes

Problem :

I have my class called MyPerson:

class MyPerson {
    string myname;
    long myscore;
    MyPerson(string myname="", long myscore=0);
    void setMyName(string myname);
    void setMyScore(long myscore);
    string getMyName();
    long getMyScore();


In other class, I have the below method:

void myprint() const {
     for (int j=0; j< nMyPlayers; j++)
     cout << "#" << j << ": " << people[j].getMyScore()//people is the array of the person objects
    << " " << people[j].getMyName() << endl;


This is my declaration of people:   

static const int mysize=8; 
MyPerson people[size];

But when I try to compile the code I face below error:

IntelliSense: the object has type qualifiers that are not compatible with the member function
with red lines under the the 2 people[j] in the print method

Am I doing something wrong here?

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

The getMyName is not the const, getMyScore is also not const, but the print is. Please make your first two const like print. You cannot call the non-const method with the const object. Since the MyPerson objects are the direct members of the other class and as you are in the const method they are considered as constants.

In general you must consider the every method you write and then declare it as const if that is what it should do. The simple getters like getMyScore and getMyName should always be made const.

38.6k points

Related questions

0 votes
1 answer 7 views
Problem: Support is needed because I have been unable to find a solution : The object has type qualifiers that are not compatible
asked Jun 25 nila 121k points
0 votes
1 answer 2 views
Problem : Object has type qualifiers that are not compatible
asked Jun 28 Sifat55 108k points
0 votes
1 answer 18 views
Problem: I'm trying to understand a concept and an error. what's wrong with this? std::function with non-static member functions
asked Apr 18 ummesalma 29.1k points
0 votes
1 answer 42 views
Problem: Does anyone grasp the problem : No instance of overloaded function matches the specified type?
asked Jun 22 Sawyer Parviz 168k points
0 votes
1 answer 10 views
Problem : How do I solve it: This project references nuget packages) that are missing on this computer?
asked Jun 16 Siam55 110k points
0 votes
1 answer 2 views
Problem: Type qualifiers ignored on function return type
asked Jul 5 asha 121k points
1 vote
1 answer 222 views
Problem: I&rsquo;m recently working on c++ but&hellip;&hellip;... I am getting the above error when I use double x = log10(100); I have used it in other class, in the same project and it does not show this error. How do I fix it? Many thanks
asked Mar 22 Sabbir Hossain 14.6k points
0 votes
1 answer 11 views
Problem: Is there any way around this : The components for communicating with ftp servers are not installed?
asked Jun 18 Nahil 128k points
0 votes
1 answer 17 views
Problem: I have no idea to solve this, somebody help : An attempt was made to reference a token that does not exist
asked Jun 23 asha 121k points