简体   繁体   中英

C++ No instance of overloaded function getline matches argument list

I'm writing a program which needs to read input from cin into a string. When I tried using the regular getline(cin, str) it endlessly prompted for input and never moved on the the next line of code. So I looked in my textbook and it said I could pass a cstring and size of the string to getline in the form of cin.getline(str, SIZE). However, when I do that I get the error "no instance of overloaded function getline matches the argument list.

I searched around, but all I found was people saying to use the getline(cin,str) form which led to the infinite input prompt, or the suggestion that there might be two different getline functions with different parameters in the classes I'm including, and that I need to tell the IDE to use the correct one(I'm not sure how to do that).

This is what I include at the beginning of my file:

#include <string>
#include <array>
#include <iostream>
#include "stdlib.h"
#include "Bank.h"   //my own class

using namespace std;

And this is the relevant section of code:

        const int SIZE = 30; //holds size of cName array
        char* cName[SIZE];   //holds account name as a cstring   (I originally used a string object in the getline(cin, strObj) format, so that wasn't the issue)
        double balance;      //holds account balance

        cout << endl << "Enter an account number: ";
           cin >> num;       //(This prompt works correctly)
        cout << endl << "Enter a name for the account: ";
           cin.ignore(std::numeric_limits<std::streamsize>::max()); //clears cin's buffer so getline() does not get skipped   (This also works correctly)
           cin.getline(cName, SIZE); //name can be no more than 30 characters long   (The error shows at the period between cin and getline)

I'm using Visual Studio C++ 2012, if that's relevant

This error message from visual studio is quite misleading. Actually for me I was trying to call a non const member function from const member function.

class someClass {
public:
    void LogError ( char *ptr ) {
        ptr = "Some garbage";
    }
    void someFunction ( char *ptr ) const { 
        LogError ( ptr );
    }
};
int main ()
{
    someClass obj;
    return 0;
}

This is the offending line:

char* cName[SIZE];

What you really need here is:

char cName[SIZE];

Then, you should be able to use:

cin.getline(cName, SIZE);

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