简体   繁体   中英

error C2664 converting from from const std::string to std::string&

I keep receiving a C2664 conversion error in visual studio

It tells me that it can't convert parameter 1 from const std::string to std::string&. I tried adding/removing the const in the stringToWstring prototype and in the function itself and the error still comes up.

    wstring hexval = buff.substr(buff.find(L"hex(2):"));
    wstring h;
    wchar_t ch;

    typedef boost::tokenizer<boost::char_separator<wchar_t> > tokenizer;
    boost::char_separator<wchar_t> sep(L"//,");

    tokenizer tokens(hexval, sep);

    for(tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); tok_iter++)
    {
        ch = someFunction(*tok_iter); //error here
        h += ch;
    }



wstring stringToWstring(const string& s)
{
    wstring temp(s.length(), L'');
    copy(s.begin(), s.end(), temp.begin());
    return temp;
}

    wchar_t someFunction(const wstring &hex_val)
{

}

Any ideas?

EDIT:

I see that this is really confusing so I'm going to explain a bit more..

Originally, what I wanted was these lines inside the for loop

ch = someFunction(*tok_iter);
h += ch

I also expected *tok_iter to return a wstring but I was getting an error like: cannot convert parameter 1 from const std::string to const std::wstring&

Because of that, I assumed that somehow, *tok_iter is a const std::string thus, I created a stringToWstring function to do the conversion. To do this in the for loop

ch = someFunction(stringToWstring(*tok_iter));
h += ch

When I did that, I got:

Error   1   error C2664: 'std::_String_const_iterator<_Elem,_Traits,_Alloc>::_String_const_iterator(const std::_String_const_iterator<_Elem,_Traits,_Alloc> &)' : cannot convert parameter 1 from 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' to 'const std::_String_const_iterator<_Elem,_Traits,_Alloc> &'    c:\program files\boost\boost_1_39\boost\tokenizer.hpp   63

I hope that's clearer now.

Looking at the edited question, your error is inside boost/tokenizer.hpp , not at the specified line.

So my guess is that your tokenizer is wrong. Looking at http://www.boost.org/doc/libs/1_34_0/libs/tokenizer/tokenizer.htm it takes three template arguments, and the third one defaults to std::string . Since you want to use it on a std::wstring , I'd say you should create your tokenizer like this instead:

tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring>

In general when debugging errors in template types, be sure to look in the Output pane, at the lines following the error. Visual Studio will tell you the types used in the templates there, allowing you to distinguish the first std::_String_const_iterator<_Elem,_Traits,_Alloc> in the error message from the second const std::_String_const_iterator<_Elem,_Traits,_Alloc> & (VC++ really isn't very good at formatting this information, but it's there)

Most likely, one of them has char for _Elem and the other has wchar_t .

I think the tokenizer iterator actually returns a wstring, not a string. You expect a string (reference) in stringToWstring function.

Or you should change the template type for tokenizer from wchar_t to char. I can't tell exactly, without the code context...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM