Type qualifiers are not compatible error

I'm trying to create a local variable of a class inside of another class, and that seems to work fine. it's a rather large project so I'm not sure what part of the code to actually show you guys, so I'll try to explain it. I basically have a main game class that has an object of type Ball as a member. I initialize that Ball object in the iniitializer list just fine. The problem is when I go to call a function that's part of that ball object/class I get an error.

 
the object has type qualifiers that are not compatible with the member function "Ball::draw" Object type is const Ball


There is nothing declared as const. Why is it saying it's const? and why can't I call the balls draw function? It's marked as public in that class.
Last edited on
Are you passing a const Ball to a non-const member function?
Post some code!
Or give a link to the whole thing.
Last edited on
@tpb
Interesting, yes I was trying to call a function of the member object Ball inside of a const function. But that function doesn't actually change anything, and neither does the function of the object that I was trying to call. I had them marked const because of that. making them non const functions fixed the issue but I don't understand why, no data was being manipulated anywhere. Here is some pseduo code of what I was trying to do originally

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Ball
{
public:
  Ball();
  ~Ball();

  void draw() {SDL_RenderCopy(......)}; 
};

class Game
{
public:
  Game();
  ~Game();

  void drawGame() const;

private:
  Ball gameBall;
}


Then in the drawGame() function I was calling gameBall.draw() and getting that error. Removing the const from the games drawGame() function fixed the issue but why? Neither "draw" functions were actually manipulating any data. the drawGame function only calls the draw functions of other game objects.
Instead of removing the const from Game::drawGame, try making both Game::drawGame and Ball::draw const functions. I think the problem is that drawGame says it's const but it calls a function on one of its members that doesn't say it's const, so it assumes that that function may alter the ball (even if it doesn't). Const correctness can be a bit of a pain!
Last edited on
Seems like a valid solution except for the draw() function for ball actually does modify some data, my mistake.
Topic archived. No new replies allowed.