• 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

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.

9 7 4
38,600 points

Related questions

0 votes
1 answer 174 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 3 views
Problem: This program has the user input name/age pairs and then outputs them, using a class. Here is the code.However, in int main() when I'm trying to call the functions I get "cannot call 'whatever name is' function without object." I'm guessing this is because it's looking for something like variable.test or variable.read_names. How should I go about fixing this?
asked Dec 13, 2020 Mashhoodch 6.7k points
0 votes
1 answer 202 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, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 41 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, 2020 Gavin 15.3k points
0 votes
1 answer 6 views
Problem: What is private member function in C++? How do you use private members in C++? What is member function with example? What is data member in C++ with example? How can i declare a function/class as friend of that particular class, and then the member will be accessible inside that function or class object without access specifier check?
asked Dec 23, 2020 Mashhoodch 6.7k points
0 votes
1 answer 56 views
Problem: Hi there! I am a bit curious about what happens if I skip the return type in function declaration. Does the compiler generate error or the program executes normally? If it executes normally, what return type is used as function return type by default?
asked Nov 28, 2020 Code Learner 5.5k points
1 vote
1 answer 51 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, 2020 morrisBson 3.2k points
0 votes
1 answer 563 views
Problem : I am using the Visual Studio 2010 in a debug mode and I have "optimize code" unchecked. I am unable to quick watch (or hover on) any variable in my debugger. I encountered following error "Cannot evaluate expression because the code of the current method ... ' as it is not available at this instruction pointer, possibly because it has been optimized away." Am I missing any setting here ?
asked Jan 18, 2020 jwilliam 3.9k points
0 votes
2 answers 516 views
Problem : I want to to run my NodeJS version with the nvm but getting following error: $ nvm use v4.2.4 nvm is not compatible with the npm config "prefix" option: currently set to "/Users/z/.npm-global"Run `npm config delete prefix` or `nvm use --delete- ... it. I have set the prefix on purpose to avoid the sudo npm How can I use nvm without losing the prefix for my globally installed packages?
asked Nov 29, 2019 alecxe 7.5k points