" cannot convert 'this' pointer" Build error

Kailric

Jack of All Trades
Joined
Mar 25, 2008
Messages
3,100
Location
Marooned, Y'isrumgone
I created a Function:

Code:
void CvCity::setExtraBuildingBonus(int iValue)
{
	m_iExtraBuildingBonus = iValue;
}

And I call the "setExtraBuildingBonus" in this another function something like this:

Code:
void CvCity::calculateNetYields(int aiYields[NUM_YIELD_TYPES], int* aiProducedYields, int* aiConsumedYields, bool bPrintWarning) const
{ 
       setExtraBuildingBonus(10);

 ....

and get this error:

Code:
CvCity.cpp(4251) : error C2662: 'CvCity::setExtraBuildingBonus' : cannot convert 'this' pointer from 'const CvCity' to 'CvCity &'

What can I do for a work around cause I am not understanding consts in functions like this?
 
"const" is one of my least favorite C++ concepts. "const" means that your function does not change anything, and if you use it correctly, it can enable some compiler optimizations. But, a const function cannot call a non-const function. Since your calculateNetYields is clearly changing stuff, remove the const from the declaration. Check to make sure your declaration in the .h and the .cpp files are matching. If you changed any .h files remove all your .obj files. Rebuild, and see if it is working.
 
On the subject of constants. Looking over the functions in CvGameCoreUtils, why aren't functions like these constant?

Code:
bool isWorldWonderClass(BuildingClassTypes eBuildingClass)
{
	return (GC.getBuildingClassInfo(eBuildingClass).getMaxGlobalInstances() != -1);
}
 
On the subject of constants. Looking over the functions in CvGameCoreUtils, why aren't functions like these constant?

Code:
bool isWorldWonderClass(BuildingClassTypes eBuildingClass)
{
	return (GC.getBuildingClassInfo(eBuildingClass).getMaxGlobalInstances() != -1);
}

Yeah, just how important is declaring something as a constant?

Anyway, in my above example CvCity::calculateNetYields has other const functions called with in it that will all need to be change to non const as well. Changeing all these functions isn't going to mess up the program some how is it?
 
A const function cannot call a non-const, because const "promises" that nothing will be changed. However, a non-const function can call a const one. That just means that the subroutine doesn't change anything but the top level function will. So if you remove const from your top level routine, you can still call const lower level routines.

Many programmers find "const" to be a pain in the neck, because of problems such as this, so they often don't bother to declare functions which "should" be const. So I am not surprised to find that some functions do not contain this declaration.
 
I suppose I'm not understanding then. In the post above, specifically for the function isWorldWonderClass, the function will always return the same for the eBuilding you pass it. If you mean that you must pass it a constant argument, this makes no sense because why have the argument in the first place. Plus we have functions like this in CvCity:
int findBaseYieldRateRank(YieldTypes eYield) const;
Which is constant, yet eYield changes, in fact it's pretty much the exact same concept as the function isWorldWonderClass, so why is one constant and the other not? What am I missing here?
 
If a function is const, that means two things: first, it doesn't change anything in the database, and second, if you call it twice in a row, you get the same result. It is a hint to the compiler that the second call is redundant and it won't change anything to delete the second call. The compiler won't generate code for the second call, and the resulting dll is a little smaller and faster. If you leave off const and don't give the hint, it just means the resulting dll is a little bigger and slower.

The devs were lazy, and left off const from isWorldWonderClass, so the compiler can't optimize away multiple calls to it. That's all.

It is the reverse situation which causes an error. If you have claimed that a function doesn't make any changes to the database (by declaring it const), but it calls some other function (non-const) which might make changes to the database, then this is an error. That function can't be declared const.

Is this making more sense now?
 
Yeah, that makes sense. So why wol't the compiler allow you to set any function in CvGameCoreUtils to const? If you try in that file, it says "Function redefinition not allowed!, see CvGameCoreUtils declarition" Doesn't make any sense to me, and I checked I had the function exactly the same in the .cpp and header. I ask because I added a function to this file (only place I could figure where to put it actually), bool canAquireExperience(eUnit) which looks at a unit and compares it's UnitCombatType to promotions infos to see if that unit type has promotions available for it, and thus is a valid UnitType to recieve experience (it's for a BUG hover that shows XP earned on units in the city build queue). Also you're saying if it would be fine to add consts to all those constant functions in CvCity? In general how much performance wise would this accomplish?
 
I don't understand the question you are asking; can you give a specific example of a function where you would like to add "const" but you get this error?

The "const" optimization only applies if the same function is called twice in a row. This is possible, but pretty unlikely. So it is not worth any effort to add missing "const". It won't change the performance noticeably. That is one reason why I consider it a big pain in the neck.
 
Const only tells the developer that the member function does not modify the instance upon which it is called. By definition this makes no sense for global functions since there is no "this" to remain unmodified. AFAIK the compiler cannot optimize multiple calls to a const function because const says nothing about not modifying the arguments passed to the function or a database or file or any other object it happens to find.

Further still, a const CvCity function will only be blocked from modifying the single instance on which it is called. If you pass in a second city, the function is allowed to modify it.

In fact, I don't see how any optimizations could be performed since you can always cast away the constness anyway.
 
Back
Top Bottom