0

I'm coding a file loader/editor and I have a problem like this: C2679: binary '=' : no operator found, which takes a right-hand operand of type std::_Vector_iterator<_Myvec> (or there is no acceptable conversion)

    std::string szName;
    szName = "";
    ReadFile(hFile, &nL, 4, &dwRWC, NULL);

    if(nL > 0)
    {
        std::vector<char> buffer(nL+1, NULL);
        ReadFile(hFile, &buffer.begin(), nL, &dwRWC, NULL);
        szName = buffer.begin(); //problem is here
    }

What could be the problem?

RepahidiS
  • 45
  • 8
  • Since you are setting `szName=""`, am I right to assume the variable is a string? If you want to get the buffer and assign it to a string, check [this](http://stackoverflow.com/questions/5115166/how-to-construct-a-stdstring-from-a-stdvectorchar). The problem, as mentioned by CoryKramer, is that you are trying to assign an iterator to a variable that is not iterator. – wendelbsilva Sep 29 '15 at 20:04
  • 3
    Hungarian notation needs to die a very painful death. – 3Dave Sep 29 '15 at 20:08
  • Thank you for your help guys Cory Kramer solved my problem with this -> "szName = *(buffer.begin());" :) – RepahidiS Sep 29 '15 at 20:11
  • Really? I doubt it, since that would result in a type mismatch. – Lightness Races in Orbit Sep 29 '15 at 20:11
  • There is still something wrong even with the answer accepted. `*(buffer.begin())` is (convertible to) a `char &`. If `szName` is a `char *` the accept solution is an assignedment of a pointer to from a `char`, which should throw warnings all over. If `szName` is a `char`, `szName = ""` is a narrowing conversion that is not acceptable at all. Why isn't the type of `szName` part of the question by now? – dhke Sep 29 '15 at 20:21
  • szName isn't a char its a string – RepahidiS Sep 29 '15 at 20:23
  • @RepahidiS Please edit your question to reflect this information. – dhke Sep 29 '15 at 20:27
  • 1
    When you say it's a string, do you mean std::string? – JPhi1618 Sep 29 '15 at 20:28
  • @dhke okey I'm editing now. – RepahidiS Sep 29 '15 at 20:32
  • @JPhi1618 yes std::string szName; – RepahidiS Sep 29 '15 at 20:32

2 Answers2

1

First of all this code is wrong:

ReadFile(hFile, &buffer.begin(), nL, &dwRWC, NULL);

you are trying to read into iterator itself, not std::vector data. This actually should not compile as you taking address of temporary which is not lvalue, but most probably compiles because of "friendly" MS extension, that allows temporary to lvalue conversion. So anyway code should be:

ReadFile(hFile, &buffer[0], nL, &dwRWC, NULL);

Assignment to string could be:

message = std::string( buffer.begin(), buffer.end() );
Slava
  • 43,454
  • 1
  • 47
  • 90
-1

begin will return an iterator, which you are trying to assign to a char. Either dereference the iterator

szName = *(buffer.begin()); 

or just call front

szName = buffer.front(); 

Edit

If szName is not a char and is instead a std::string, you should actually do something like the following to construct a std::string

szName = std::string(buffer.begin(), buffer.end());
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Thank you so much its worked! :) – RepahidiS Sep 29 '15 at 20:03
  • 2
    Would be nice to remind him that, if `szName` is a string and not a char, he will be assigning only the first char of the vector. Im pointing this out because in his snippet, he used `szName=""` and not `szName=' '`. – wendelbsilva Sep 29 '15 at 20:09
  • Is it a `char`? Doesn't look that way to me. – Lightness Races in Orbit Sep 29 '15 at 20:12
  • szName is a string guys not a char – RepahidiS Sep 29 '15 at 20:13
  • Can you update this answer to reflect that `szName` is an `std::string`, and indicate that the assignment actually calls [`operator=(char c)`](http://www.cplusplus.com/reference/string/string/operator=/`)? As it stands this answer is wrong in the first paragraph. – dhke Sep 29 '15 at 20:36