【问题标题】:receiving error "no matching function for call to 'getline(std::ifstream&, int&, char)'"接收错误“没有匹配函数调用'getline(std::ifstream&, int&, char)'”
【发布时间】:2015-11-16 03:20:57
【问题描述】:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct subscriberName
{
   string first;
   string last;
   int custID;
};

struct address
{
   string address2;
   string city;
   string state;
   int zipcode;
};

struct date
{
   string month;
   int day;
   int year;
};

struct renewal_information
{
   int monthsLeft;
   date da;
};

struct subscriberInfo
{
   subscriberName si;
   address ad;
   renewal_information ri;
};


int main()
{
   void OpenFileIn(ifstream& FileIn, string& FilenameIn);
   void OpenFileOut(ofstream& FileOut, string& FilenameOut);
   bool ProcessCustInfo(bool& truth, ifstream& FileIn);
   void OutputCustInfo(ifstream& FileIn, ofstream& FileOut);

   ifstream FileIn;
   ofstream FileOut;
   string FilenameIn;
   string FilenameOut;
   bool truth;
   subscriberInfo si;


   OpenFileIn(FileIn, FilenameIn);

   OpenFileOut(FileOut, FilenameOut);

   ProcessCustInfo(truth, FileIn);

   OutputCustInfo(FileIn, FileOut);
   return 0;
}

bool ProcessCustInfo(bool& truth, ifstream& FileIn, subscriberInfo& si)
{
   getline(FileIn, si.sn.first, '\n');                   //here
   getline(FileIn, si.sn.last, '\n');
   getline(FileIn, si.sn.custID, '\n');
   getline(FileIn, si.ad.address2, '\n');
   getline(FileIn, si.ad.city, '\n');
   getline(FileIn, si.ad.state, '\n');
   getline(FileIn, si.ad.zipcode, '\n');
   getline(FileIn, si.ri.monthsLeft '\n');        //to here




}



void OutputCustInfo(ifstream& FileIn, ofstream& FileOut, subscriberInfo& si)
{
   if(si.ri.monthsLeft=0)                     //here down to
   {
      FileOut << string(55,'*') << endl;
      FileOut << si.sn.first << " " << si.sn.last << "(" << si.sn.custID << ")" << endl;
      FileOut << sn.ad.address2 << endl;
      FileOut << sn.ad.city << ", " << sn.ad.state <<sn.ad.zipcode << endl;
      FileOut << "The last renewal notice was sent on " <<sn.ri.da.month << " " << sn.ri.da.day << ", " << sn.ri.da.year << endl;          //here
      FileOut << string(55,'*') << endl;
   }
}

我不知道是什么导致了这个错误。它发生在所有 getline 调用所在的第一个函数中。编译器专门调用了第三个、第五个和最后一个,但我很确定它们都有问题。

【问题讨论】:

  • geline() 只接受string&amp; 作为第二个参数,您需要将其放在字符串中,然后将其转换为int 或其他类型。由于您的结构的某些成员的类型为int
  • @JohnMarkCaguicla 将成员更改为字符串会更容易吗?这会导致什么问题?
  • 这取决于您将如何使用它们,如果您只是显示它们,可以将它们更改为string,但为了更实际的目的,您可以,例如,使用custID 作为 int 传递给函数。在这种情况下,只需将其检索为 string,然后将其转换为 int
  • 您可以简单地使用并重载 ofstream 和 ifstream 的插入和提取运算符。他们会自动转换类型。

标签: c++ compiler-errors getline


【解决方案1】:

您将int 类型的变量传递给getline

getline(FileIn, si.sn.custID, '\n');

这是个问题。

用途:

std::string custID;
getline(FileIn, custID, '\n');
si.sn.custID = std::stoi(custID);

你有同样的问题:

getline(FileIn, si.ad.zipcode, '\n');

getline(FileIn, si.ri.monthsLeft '\n');

还有一行

if(si.ri.monthsLeft=0)

错了。我怀疑这是一个错字。您需要使用== 而不是=

if(si.ri.monthsLeft == 0)

【讨论】:

  • 是的,谢谢。 std::stoi(custID); 到底是做什么的?
  • @LCr,它将字符串转换为整数。您可以在en.cppreference.com/w/cpp/string/basic_string/stol查看更多详细信息。
  • 另外,我只能有 3 个参数 bool,我怎样才能让 custIDzipcodemonthsLeft 超出该范围?
  • @LCr,我不明白。
  • 对不起,我每个函数只能有 3 个参数。我再次收到一个编译器错误,提示 custIDzipcodemonthsLeft 未在函数范围内声明。我该如何解决?
猜你喜欢
  • 2020-07-14
  • 2020-07-02
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
相关资源
最近更新 更多