1

I was working on some basic code but had problems with my getline() function. I was using a while loop to go through a stringstream like this:

    while (N > -1){
        getline(cin, s);
        stringstream ss(s);
        
      while (getline(ss, n, ",")){
            num.push_back(n); 
      }   

I always go this warning saying “no instance of overloaded function “getline” matches the argument list —- argument types are:(std::stringstream, int, const char[2])”

I thought it was my code that was the problem, but when I tried copying and pasting Geeks for geeks code into visual studio code I got a similar message.

Here’s their code:


#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string str = "1, 2, 3, 4, 5, 6"; 
    vector<string> v; 
  
    stringstream ss(str); 
  
    while (ss.good()) { 
        string substr; 
        getline(ss, substr, ', '); 
        v.push_back(substr); 
    } 
  
    for (size_t i = 0; i < v.size(); i++) 
        cout << v[i] << endl; 
}

I’ve also seen a YouTuber, Derek Banas, do something similar in his recent C++ tutorial. When I try to copy him, I always get an error.

I’m not sure what I’m doing wrong.

Becky B
  • 11
  • 2
  • `#include ` -- So the youtube video shows to use this header? This is why it isn't a good idea to learn C++ from videos that have not been peer-reviewed. – PaulMcKenzie Aug 20 '20 at 02:39
  • To understand the duplicates better, I guess you're on a POSIX system (e.g. Linux or macOS) which have a *different* [`getline`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html) function in the global namespace. By including that header-file you also get the header fie declaring that POSIX function, and the `using` statement then pulls in the `std::getline` overloads into the same global namespace, making them all clash. – Some programmer dude Aug 20 '20 at 02:41
  • Please always post the *full* error message. Your code uses `', '`, a comma then a space, which is not a valid character. Get rid of the extra space character. – Ken Y-N Aug 20 '20 at 02:41
  • 1
    Pretty sure this was closed for the wrong reason. It has nothing to do with bits/std++.h and using namespace std. It *is* a typo, since the `getline` call passes ", ". – Stephen Newell Aug 20 '20 at 02:42
  • 1
    Also, please make your questions consistent. The first snippet of code doesn't match the other code you show. The first snippet also uses the wrong single quotes. Please copy-paste properly. – Some programmer dude Aug 20 '20 at 02:42
  • @StephenNewell The [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) function have overloads using both characters and strings. – Some programmer dude Aug 20 '20 at 02:43
  • @Someprogrammerdude Agreed, which is why I think this should be closed as a typo, not a duplicate. – Stephen Newell Aug 20 '20 at 02:46
  • To the OP (that's you Becky B) please [edit] your question to include a single [mcve], and a complete and full copy-paste (as text) of the build output. – Some programmer dude Aug 20 '20 at 02:48
  • I’ve fixed the first example. The reason they aren’t exactly the same is because the first one is my code and the second is one I found online. I no longer have problems with the code I found online after taking your advice, but I still get problems with mine. – Becky B Aug 20 '20 at 03:03
  • Also for my code, I wasn’t using the `#include ` so I don’t think that was the problem. – Becky B Aug 20 '20 at 03:07

0 Answers0