0
string strMap[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; //string digits received as parameter in function // digits = "234"; 

Accessing it as below:

int digitmapIndex = digits[index] - 46; //index is some int 0,1 etc.. int mapstr_len = strMap[digitmapIndex]; 

or

int mapstr_len = strMap[digits[index] - 46]; 

Error:

Line 25: Char 13: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'int' int mapstr_len = strMap[digits[index] - 46]; ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:816:7: note: candidate function operator __sv_type() const noexcept 
3
  • 4
    strMap[digitmapIndex] is a std::string the compiler does not have a way to automatically convert it to an integer. Commented Feb 11, 2021 at 20:06
  • 2
    By your variable naming did you want: int mapstr_len = strMap[digitmapIndex].length(); Commented Feb 11, 2021 at 20:09
  • Yes ..got it. silly mistake Commented Feb 11, 2021 at 20:13

1 Answer 1

5

This line makes no sense:

int mapstr_len = strMap[digitmapIndex]; 

strMap[digitmapIndex] is a string object, and cannot be assigned to an int.

auto temp = strMap[digitmapIndex]; 

From temp, you can access its chars by index.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.