-6

I've got 2 errors that makes me sick and a little bit confused.

Error #1:

error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

Code to this error is:

CString lancuch1;   
lancuch1  = "Znaleziono ";
lancuch1  += liczba1.str();
lancuch1  += " pozycji.";

And the second one, more weird:

Error #2:

error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'std::basic_string<_Elem,_Traits,_Ax>'

And this error i've got 7 times written to this code:

for(int i = 0 ; i < pojemnosc_vectora; i++){ 
std::string linijka = (vector.begin()+i); 
char deli = ';'; 
int a = 0; 
for(int i = 0; i<5; i++){ 
std::string pokico = linijka.substr(a, deli); 
vector2.push_back(pokico); 
a+=pokico.length(); 
} 
}
int licznik_komunikatow=0;
for(int i=0; i<vector.size(); i++){
std::string komunikat1 = vector2.begin()+(licznik_komunikatow);
std::string komunikat2 = vector2.begin()+(licznik_komunikatow+1);
std::string komunikat3 = vector2.begin()+(licznik_komunikatow+2);
std::string komunikat4 = vector2.begin()+(licznik_komunikatow+3);
std::string komunikat5 = vector2.begin()+(licznik_komunikatow+4);
CString komun,komun1,komun2,komun3,komun4;
komun = komunikat1.c_str();
komun1 = komunikat2.c_str();
komun2 = komunikat3.c_str();
komun3 = komunikat4.c_str();
komun4 = komunikat5.c_str();
printf("Nazwa: %s \n Cena: %s \n Ilość: %s \n Gdzie: %s \n Kod: %s \n ", komun, komun1, komun2, komun3, komun4 );                   
}

Tell me is it my bad or Visual's 2005 bad. I'm a little bit tired of weird error's that I don't really understand. Anyone have an Idea how to fix this?

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 3
    "my bad or Visual's 2005 bad" --> 99.98% of the time, it is the coder's mistake. (maybe 99.0% with [VS 2005](http://stackoverflow.com/questions/38003015/why-my-compiler-is-showing-some-errors-that-shouldnt-exist#comment63450118_38003015)) – chux - Reinstate Monica Jun 23 '16 at 22:42
  • 1
    Firstly, dont use Visual Studio 2005. Its really old and massively buggy compared with newer versions. VS2015 community edition is a free download. – Mike Vine Jun 23 '16 at 22:42
  • 1
    `(vector.begin()+i);` will give you an iterator that you'll have to dereference to get what's inside. Presumably a `std::string` – user4581301 Jun 23 '16 at 22:43
  • I would like to work on Visual 2015, but I have to do it in 2005 because of Windows CE... I know V '05 is bad but when I have to work on it I must.. – Anthony Martial Jun 23 '16 at 22:43
  • Please indent your code. – Jabberwocky Jun 24 '16 at 07:12
  • *"I'm a little bit tired of weird error's that I don't really understand."* - Easy solution: Learn the programming language you are using. Start with [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Jun 24 '16 at 09:51
  • Thank you very much for good practice source, Ilnspectable.! – Anthony Martial Jun 24 '16 at 11:28

1 Answers1

5

The first is pretty simple: you're trying to mix Micrsosoft's CString with std::string, but they know nothing about each other. As a general rule, you probably want to pick one and use it exclusively (I'd prefer std::string as a rule, but if you're using other parts of MFC or ATL that use CString, you may not have much choice).

If you really can't avoid mixing them, you could use c_str() to get a C-style string that CString knows how to use:

CString lancuch1;   
lancuch1  = "Znaleziono ";
lancuch1  += liczba1.str().c_str();
lancuch1  += " pozycji.";

Or (preferred):

std::string lanuch1 = "Znaleziono "s  + liczba1.str() + " pozychi."s;

For the second, it sounds like you've failed to dereference an iterator where you needed to, but since you don't point to the specific part of the code generating the error, it's a bit hard to say more with certainty. Probably in these lines though:

std::string komunikat1 = vector2.begin()+(licznik_komunikatow);
std::string komunikat2 = vector2.begin()+(licznik_komunikatow+1);
std::string komunikat3 = vector2.begin()+(licznik_komunikatow+2);
std::string komunikat4 = vector2.begin()+(licznik_komunikatow+3);
std::string komunikat5 = vector2.begin()+(licznik_komunikatow+4);
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Second one is the line `std::string komunikat1 = vector2.begin()+(licznik_komunikatow);` – Mike Vine Jun 23 '16 at 22:45
  • @AnthonyMartial: Like the error says, you cannot assign a `std::vector::iterator` to a `std::string`, but that is what you code is trying to do (in many places). Your `vector` contains `std::string` elements, so just dereference the iterator: `std::string komunikat1 = *(vector2.begin()+(licznik_komunikatow));`, or stop using iterators and just use the `vector`'s `[]` operator instead since you are using indexes anyway: `std::string komunikat1 = vector2[licznik_komunikatow];` – Remy Lebeau Jun 24 '16 at 02:41