Getline in class c++

Getline in class c++

本文关键字:c++ class in Getline      更新时间:2023-10-16

我正在尝试编写一个getline函数,该函数将从打开的流中接收一串单词...我查看了不同的网站并尝试了它所说的操作,但我遇到了大量奇怪的错误。我需要获取线来接收文件"内容"部分中的所有单词。这是我尝试过的,但我遇到了错误。

// Fill a single Tweet from a stream (open file)
// Returns true if able to read a tweet; false if end of file is encountered
// Assumes that each line in the file is correct (no partial tweets)
bool Tweet::FillTweet(ifstream &Din)
{
   string tmp;
   bool Success = false;
   Din >> tmp;
   if (!Din.eof())
   {
      Success = true;
      date = tmp;
      Din >> hashtag >> getline(Din,contents);
   }
   else
      cout << "I don't want to read your tweet anyway. " << endl;
这是

使用getline()的错误方式。更改为:

Din >> hashtag;
getline(Din,contents);

为什么? getline()返回对 istream 的引用。没有需要两istream秒的重载>>运算符

这个

  Din >> hashtag >> getline(Din,contents);

需要:

  Din >> hashtag;
  getline(Din, contents);

我也会做

if (Din >> tmp)

而不是if (!Din.eof)