2

I am trying to write a < string > into a vector - I did this exact thing with a vector of int and it worked OK - so I suspect I need to do this differently when using a string - but While I understand my compile error is telling me that I am trying to use incompatible types < char > - I don't know why because I THOUGHT I had declared everything and used it correctly - can someone please tell me how I got this wrong?

Here is the code I am trying to use:

#include <vector>
#include <iostream>
#include <string>

int main()
{
   std::vector<std::string> strArray;
   std::vector<int>::const_iterator y;

   std::string iString = "";

   while (iString != "quit")
   {
      std::cout << "Enter string: ";
      std::cin >> iString;
      iString.push_back(iString);
   }

   std::cout << "Your list is:\n";
   for (y= iString.begin(); y <= iString.end(); y++)
   {
      std::cout << (*y)
                << std::endl;
   }
   std::cout <<  std::endl;

   return 0;
}

And here is the error message:

check10a.cpp: In function ‘int main()’:
check10a.cpp:57:32: error: no matching function for call to ‘std::basic_string<char>::push_back(std::string&)’
       iString.push_back(iString);

I would really appreciate a bit of a pointer here...

2
  • 2
    iString.push_back(iString);? You mean strArray.push_back(iString);? Commented Mar 13, 2019 at 10:48
  • And here I thought I didn't understand something - and it was just a simple case of coding too late at night and not seeing what was in front of my eyes! THANK YOU HEAPS for taking the time to comment! Commented Mar 13, 2019 at 10:50

2 Answers 2

3

The compiler tells you that std::basic_string<char>::push_back with std::string argument doesn't exist.

Use operator+=: iString += iString.


You probably want strArray.push_back(iString).

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

2 Comments

Hi Maxim - FYI I SO appreciate your patience with answering such a simple mistake. I was definitely stuck, and not seeing what was right in front of my nose! ANd this was enough of a kick that I wa able to also find my other error - fix them both and submit my homework. Thank You!
@kiltannen Aww, that happens to me as well all the time!
1

Just for the benefit of anyone else who might find this - therre was another error in my code, where in fact I had not understood a piece of the required concept around iterators:

this line here:

std::vector<int>::const_iterator y;

in fact needed to be:

std::vector<std::string>::const_iterator y;

If I understand this right - it is because the iterator being declared needs to be type compatible with the collection it is iterating over...

4 Comments

Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review
@Dave Hi Dave, THX for the feedback, A couple of times I have been encouraged to provide answers as standalone rather than as edit's in the question. And while this is not a complete answer - I suspect it might actually be more confusing for someone if I embedded it into the question. I suppose I could completely replace the bad code - and just have this question focus on the piece that someone else provided the answer for - But when I figured out I had this problem I thought it might actually help someone else almost as much as my original question. So I chose to specifically call it out...
My 2 cents - reword "needs to understand what form of class it is iterating over" to "needs to be type compatible with the collection it is iterating over" - if you make that change I'll upvote it.
@codenheim Thank you for the suggestion - it says it much better than I tried to. And I even understand it better myself this way!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.