Cannot Convert 'this' Pointer

Hi! I've been developing a CBox class with all kinds of operations working with on Boxes and I'm getting a compiler error I don't understand. The error is "'CBox::volume' : cannot convert 'this' pointer from 'const CBox' to 'CBox &'
1> Conversion loses qualifiers"

I'm getting this 7 times for all of my overloaded operators, what's happening here?

Here's the global overloaded function definitions in "BoxOperators.cpp":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "Box.h"

// overload every possible operator for comparing CBox objects
// as well as % and * operators
bool operator>(const double& value, const CBox& aBox)
{ return value > aBox.volume(); }

bool operator<(const double& value, const CBox& aBox)
{ return value < aBox.volume(); }

bool operator>(const CBox& aBox, const double& value)
{ return value < aBox; }

bool operator<(const CBox& aBox, const double& value)
{ return value > aBox; }

bool operator>=(const double& value, const CBox& aBox)
{ return value >= aBox.volume(); }

bool operator<=(const double& value, const CBox& aBox)
{ return value <= aBox.volume(); }

bool operator>=(const CBox& aBox, const double& value)
{ return value <= aBox; }

bool operator<=(const CBox& aBox, const double& value)
{ return value >= aBox; }

bool operator==(const double& value, const CBox& aBox)
{ return value == aBox.volume(); }

bool operator==(const CBox& aBox, const double& value)
{ return value == aBox; }

CBox operator*(int n, const CBox& aBox)
{ return aBox*n; }

double operator%(const CBox& aBox, const CBox& bBox)
{ return aBox.volume() - (aBox/bBox) * bBox.volume(); }


Their declarations in "BoxOperators.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once

bool operator>(const double& value, const CBox& aBox);
bool operator<(const double& value, const CBox& aBox);
bool operator>(const CBox& aBox, const double& value);
bool operator<(const CBox& aBox, const double& value);
bool operator>=(const double& value, const CBox& aBox);
bool operator<=(const double& value, const CBox& aBox);
bool operator>=(const CBox& aBox, const double& value);
bool operator<=(const CBox& aBox, const double& value);
bool operator==(const CBox& aBox, const double& value);
bool operator==(const double& value, const CBox& aBox);
CBox operator*(int n, const CBox aBox);
double operator%(const CBox& aBox, const CBox& bBox);


And the class definition in "Box.h" if it helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#pragma once

class CBox
{
public:
	CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0);
	~CBox(void);

	// accessor functions for the dimensions
	double getHeight(void)
	{
		return m_height;
	}
	double getWidth(void)
	{
		return m_width;
	}
	double getLength(void)
	{
		return m_length;
	}

	double volume(void)
	{
		return m_height*m_width*m_length;		// return the volume of an object
	}
	
	//overloaded operators
	CBox operator+(const CBox& aBox) const;
	CBox operator*(int n) const;
	int operator/(const CBox& aBox) const;
private:
	double m_length;
	double m_width;
	double m_height;
};	   


I didn't write a copy constructor so I passed everything by reference. Is that why it's having trouble converting them? Thank you!
Last edited on
If a member function doesn't modify the class, you must declare it as const:
double volume(void) const
Otherwise, the compiler won't let you call it from a const instance of the object.
Thank you! That fixed it :)
No problem. It's good to get into the habit of putting those consts on the end where applicable.

(EDIT: Remember to mark the thread as solved ;) )
Last edited on
Yeah I'm not sure why I made the +, * and / operators const but forgot to do it for the accessor and volume functions... stupid me. I threw those in anyhow so thank you :D
Last edited on
Making those operators const is fine, as they don't modify the existing variable (I hope!), just create a new one as the sum/product/quotient of two existing ones.

On the other hand, do not make operator+=, operator*= etc const!
I haven't implemented += and *= operators yet. My point was just I didn't understand why I remembered to make those operators const but not that accessor and volume functions. The +, * and / operators don't modify the existing object of course, I'm not that stupid :D
I wasn't suggesting that you were stupid ;) And sure - I just slightly misunderstood your previous post... :P
I was suggesting that I was stupid :o
Topic archived. No new replies allowed.