Please read the error message:
1. the message explicitely refers to a line of code. You're making it harder on us and yourself by not checking and providing that info: we'll have to guesss what part of the code the error message is referring to
2. The (second) type mentioned isn't
const char
, it is
const char*
, meaning pointer to constant character. It
may refer to a char array or C-style string. In any case the error message states it is trying to find a conversion to this type, i. e. there is an assignment to a variable compatible with this type, or a function call expecting an argument compatible to this type. Looking at your code this may refer to either the first or the fifth line.
3. the first type mentioned is
int
, so it may refer to any variable or literal that is of type int, or an expression that has the result type of int. In your code this may refer to the value that you explicitly cast to
int
in the fifth line of your code. Or it may refer to the first line, if the variable
addToNext
is of type
int
.
4. The cause of the error is the use of a variable or expression of inappropriate type, in a place where a different type is expected, and no conversion available. In both lines of your code that could have caused the error you are using C-style type casts. But since the conversion in question is between incompatible types, no cast will remedy the situation! The question arises why you did use those casts in the first place? Chances are that you don't really understand what type casts do: I wouldn't be surprised if both lines are semantically wrong.
Solution:
a) Read up on types and casts, and specifically on the new style casting operator
static_cast
. Don't ever use C-style casts on (or to) C++ classes! An advanced read on this is
http://www.gotw.ca/gotw/017.htm[
^], but you can find tutorials and discussions on each of the new casting operators anywhere on the web, and at all levels of difficulty (and comprehensibility).
b) rethink the purpose of your code, what each variable is supposed to hold, and if you are actually using the correct type for each variable. If you think you need a type cast at all, chances are your code is wrong, or at the very least you've chosen the wrong type for your variables!
c) work on your code until you can do without type casts. Once you succeed in doing that, there is a good chance you actually understand what is going on as you pass the values around.
d) You may find that you can't do what you intended (without type casts), even after considering b) and c). If you arrive at this point, go back to a) and make a point to truly understand what a type cast can do.
And what a type cast can not do! Hint: the conversion you need does not exist, so a type cast won't help. Instead you need a conversion function (see link in solution 1)
e) Make it a habit to never use type casts! At the very least never use C-style type casts. In my experience, type casts are almost always a sign of bad code. Sometimes they're necessary. But most of the time, you should not need to cast anything. If you think you can't avoid it, then likely something in your code is wrong! Or, as in this case, you're trying to accomplish something that simply doesn't work that way.