My solution with an error that (see error in the description:).can someon help me ?


  • 0
    L
    error: "Line 30: invalid user-defined conversion from ‘char’ to ‘std::map<std::basic_string<char>, int>::key_type&& {aka std::basic_string<char>&&}’ [-fpermissive]"..     
       class Solution {
        public:
            int romanToInt(string s) {
           
        	    map<string,int>query;
        	//    vector<map<string,int> >vec;
        	    
        	    query.insert(make_pair("M",1000));
        	    query.insert(make_pair("CM",900));
        	    query.insert(make_pair("D",500));
        	    query.insert(make_pair("CD",400));
        	    query.insert(make_pair("C",100));
        	    query.insert(make_pair("XC",90));
        	    query.insert(make_pair("L",50));
        	    query.insert(make_pair("XL",40));
        	    query.insert(make_pair("X",10));
        	    query.insert(make_pair("IX",9));
        	    query.insert(make_pair("V",5));
        	    query.insert(make_pair("IV",4));
        	    query.insert(make_pair("I",1));
        	    
        	    
        	    int sum=0;int i=0;
        	    int len=s.size();
        	    string substr;
        	    while(i<len-1)
        	    {
        	        substr="";
        	        if(query[s[i]]<query[s[i+1]])
        	        {
        	            substr+=str[i]+str[i+1];
        	            i=i+2;
        	        }
        	        else 
        	        {
        	            substr=s[i];
        	            i++;
        	        }
        	        sum+=query[substr];
        	    }
        	    sum+=query[s[i]];
        	    return sum;
        	    
            }
        };
        Line 30: invalid user-defined conversion from ‘char’ to ‘std::map<std::basic_string<char>, int>::key_type&& {aka std::basic_string<char>&&}’ [-fpermissive]

  • 1
    S

    The error message is enough to understand.

    Line 30: invalid user-defined conversion from ‘char’ to ‘std::map<std::basic_string<char>, int>::key_type&& {aka std::basic_string<char>&&}’ [-fpermissive]"..

    The code defines query as map<string,int> which means the type of key should be string. In the following code, query is using by if(query[s[i]]<query[s[i+1]]). s[i] is not a string but a char. That is exact what the message means.


  • 0
    L

    thx~i use the s[i],which is a char...i forgot it ,


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.