Click here to Skip to main content
14,694,017 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
string stringAdd = (string) addToNext;

for (unsigned int i = stringAdd.length(); i>0; --i)
{
    endvec.push_back((int)(stringAdd.at(i)-'0'));
}


I am not sure why this doesn't work.

I am getting this error code:

error: invalid conversion from 'int' to 'const char*' [-fpermissive]


I am not sure what is meant by const char? Does that mean c-style string (array of chars)?
Posted
Comments
   
Please take a C++ book and read it, before asking such questions. And close the troll gate.
—SA
[no name] 2-Mar-14 23:40pm
   
Can't answer until you tell us how endvec is declared.
LLLLGGGG 3-Mar-14 10:51am
   
Use atoi function

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.
   
   
Try this

string stringAdd = (string) addToNext;
 
const char *ptrChar = null;
for (unsigned int i = stringAdd.length(); i>0; --i)
{
    ptrChar = stringAdd.at(i);
    char tmp = *Char - '0';
    endvec.push_back((int)tmp);
}
   
I guess you declared addToNext as an int, hence
Quote:
string stringAdd = (string) addToNext;
is plain wrong.

You could write:

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
  int addToNext = 5000;
  ostringstream oss;
  string stringAdd;
  vector <int> endvec;

  oss << addToNext;
  stringAdd = oss.str();

  for (unsigned int i = stringAdd.length(); i>0; --i)
  {
    endvec.push_back((stringAdd.at(i-1)-'0'));
  }

  for (int i = 0; i<endvec.size(); ++i)
    cout << endvec[i] << endl;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900