• 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?

6 5 3
7,540 points

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.

9 7 4
38,600 points

Related questions

0 votes
1 answer 41 views
Problem: I have decent knowledge in C++. Here I am trying to write the quicksort please find below my C++ code: void swap(int& a, int& b); int mid(int lo, int hi); // Below is my quicksort implementation void sort(int vec[], int lo, int hi) {         int ... = mid(lo, hi);                     ~~~^ 1 error generated. Everything seems to be correct. Can someone help me in fixing my above C++ code?
asked Jul 22 Raphael Pacheco 4.9k points
0 votes
1 answer 55 views
Problem: I want to use the three threads to execute a non static member function of the objects contained within another object. My code does not compile. "Call to non-static member function without an object argument." I face the above error if I try to pass the pointer to each ... a.add_b(2); a.add_b(3); a.do_someting(); return 0; } Looking for guidance from C++ expert to fix my above code.
asked Aug 11 Raphael Pacheco 4.9k points
1 vote
1 answer 27 views
Problem: First of all, I apologize to all of you guys. It might be a noob question but I couldn't figure out the problem. I was trying to print some data from a class file in my main function. The program never returned my expected result but an error cannot call member ... endl; } }; int main(){ Student::printValue(); return 0; } Which point I am doing wrong? Could you please sort it out?
asked Apr 8 Gavin 15.3k points
1 vote
1 answer 34 views
Problem : I am unable to build my projects as I am facing below message: &ldquo;Unable to find manifest signing certificate in the certificate store&rdquo;. I have tried to run my Visual Studio as the Administrator and also I have tried to manually add my keys to ... Project Properties but above error is still appearing. I am unable to fix above error. Can someone guide me in fixing above error?
asked Apr 4 morrisBson 3.2k points