• Register
0 votes
389 views

Problem :

I have my class called MyPerson:

class MyPerson {
    string myname;
    long myscore;
public:
    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

1 vote
1 answer 98 views
98 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 Shovo210 13.3k points
0 votes
1 answer 2 views
2 views
Problem: Tell me about that attempt which was made to reference a token that does not exist .
asked 13 hours ago salim1 34.8k points
0 votes
1 answer 4 views
4 views
Problem: Getting &ldquo;type or namespace name could not be found&rdquo; but everything seems ok?
asked Mar 16 ummesalma 24.5k points
0 votes
1 answer 20 views
20 views
Problem: are you missing a using directive or an assembly reference.
asked Feb 18 charles mathews 5.5k points
0 votes
1 answer 25 views
25 views
Problem: I am doing a C ++ wrapper for a C library. I don't want to include the C headers within them .hpp, so I work with type predeclaration. Also, trying not to commit too many undefined behaviors, most of my pointers are void *, and I am shaping as necessary. I have a curious ... public Base< uv_loop_t * > { bool test( ) const { return uv( ); } }; What am I doing wrong ? How do I solve it ?
asked Feb 7 sasha 16.2k points
1 vote
1 answer 3 views
3 views
Problem: Why I got the error : The name 'InitializeComponent' does not exist in the current context. I create a new project in Visual Studio 2010 SP1 and select "WPF Application" .When I tries to build the generated its show Error.
asked Mar 20 Shovo210 13.3k points
0 votes
1 answer 14 views
14 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 13k points
0 votes
1 answer 371 views
371 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 91 views
91 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