【问题标题】:no matching function for call to getline调用 getline 没有匹配的函数
【发布时间】:2015-02-01 17:33:48
【问题描述】:

您好,我正在开展一个学校项目,从文件中读取文本,然后对其进行操作,以使输出有所不同。无论如何,当我尝试使用 getline 函数时,我得到一个“getline 没有匹配函数”。我不明白为什么,因为我似乎为函数使用了正确的参数。关于为什么这不起作用的任何想法?

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <algorithm>
using namespace std;

bool isaNoun( const string& noun );

bool isanArticle( string value, string * container, int size);

int main(int argc, char * argv[])
{    
    char vowels[]={'a','e','i','o','u', 'A', 'E', 'I', 'O', 'U'};

    string articles[]={ "a", "A", "an", "An", "aN","AN", "the", "The", "tHe", "thE", "THe", "tHE", "THE"};
    int stringsize=sizeof(articles)/sizeof(articles[0]);// determine size of array
    istream *adr;//location of file to be read.
    string adjective=argv[1];
    cout<<adjective;
    ifstream infile;

    if (argc<=2)
        cout<<"Not enough arguments given"<<endl;
    else if(argc==3)//this is where the magic happens.
    {
        infile.open(argv[2]);

    }
    if (infile.is_open())
        adr=&infile;
    string aLine;
    getline(&adr, aLine);
    if(adr->good)
        cout<<oLine<<endl;

    return 0;
}
//function to check if input is a noun
bool isaNoun( const string& noun){

    return isalpha(noun[0]);
}

bool isanArticle( string value, string * container, int size)
{

    for(int i=0; i<size;i++)
    {
        if (value==container[i])
        {
            return true;
        }
        cout<<i<<endl;
    }    
    return false;
}

【问题讨论】:

  • 您需要整理出您的地址 (&amp;) 和取消引用 (*) 运算符。您还需要考虑如果您初始化adr 会发生什么。以及如何调用成员函数。

标签: c++ getline


【解决方案1】:

使用getline(*adr,aLine); 而不是getline(&amp;adr,aLine) 因为 getline 不能接受对变量或文件的引用作为其参数。

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 2018-10-09
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2020-10-21
    相关资源
    最近更新 更多