读取文件直到空行

发布于 2024-10-07 14:54:41 字数 1564 浏览 3 评论 0原文

阅读Jerry Coffin对此的回答问题 我将他的代码复制粘贴到我的编辑器中,经过一些细微的编辑后,它按预期编译并运行。

以下是更改后的代码:

 #include <iostream>
 #include <string>
 #include <istream>
 #include <fstream>

class non_blank {
private:
 std::string data_;

 friend std::istream& operator>> (std::istream &is, non_blank &n) {
  std::getline(is, n.data_);

  if (n.data_.length() == 0) {
   is.setstate(std::ios::failbit);
  }
  return is;
 }

public:
 operator std::string() const {
  return data_;
 }
};


int main(int, char *[]) {

 non_blank line;
 std::ifstream ifs("teste.txt");

 while(ifs >> line) {
  //std::cout << line; <----- error
  std::string s = line;
  std::cout << s << std::endl;
 }

 return 0;
}
  • 尝试在 std::cout <<... 表达式中使用 non_blank 变量时出现错误。难道我不应该能够在任何要使用 std::string 的地方使用 non_blank 类型的变量吗?这不是转换/转换运算符的目的吗? 答案

  • 为什么我无法访问私有变量字符串: :data_直接在operator>>的定义中?

这是我得到的错误:

..\main.cpp: In function `std::istream& operator>>(std::istream&, non_blank&)':
..\main.cpp:21: error: invalid use of non-static data member `non_blank::data_'
..\main.cpp:26: error: from this location

After reading Jerry Coffin's answer on this question I copy-pasted his code into my editor, and after some minor edits it compiled and run like it should.

Here is the code after the changes:

 #include <iostream>
 #include <string>
 #include <istream>
 #include <fstream>

class non_blank {
private:
 std::string data_;

 friend std::istream& operator>> (std::istream &is, non_blank &n) {
  std::getline(is, n.data_);

  if (n.data_.length() == 0) {
   is.setstate(std::ios::failbit);
  }
  return is;
 }

public:
 operator std::string() const {
  return data_;
 }
};


int main(int, char *[]) {

 non_blank line;
 std::ifstream ifs("teste.txt");

 while(ifs >> line) {
  //std::cout << line; <----- error
  std::string s = line;
  std::cout << s << std::endl;
 }

 return 0;
}
  • I've got an error when trying to use a non_blank variable in a std::cout <<... expression. Shouldn't I be able to use a variable of the type non_blank anywhere I would use a std::string? Isn't it the purpose of the cast/conversion operator?? answer

  • Why can't I access the private variable string::data_ directly in the definition of the operator >>?

Here is the error I got:

..\main.cpp: In function `std::istream& operator>>(std::istream&, non_blank&)':
..\main.cpp:21: error: invalid use of non-static data member `non_blank::data_'
..\main.cpp:26: error: from this location

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2024-10-14 14:54:41

我不应该使用变量吗
我想要的任何地方的 non_blank 类型
使用 std::string 吗?难道不是
强制转换运算符的用途?

不完全是。如果编译器发现您正在做一些它知道需要 std::string 的事情,它可以调用您的转换运算符来获取它。但对于 ostream 运算符 << 来说,它没有一个特定的函数可供调用,而是有相当多的函数,它们全部不同,并且没有一个与您要打印的实际类型精确匹配。因此它列出了一大堆候选者,但没有一个是足够强大的匹配。您需要定义一个 ostream 运算符 <<适合您的类型,以便使其按应有的方式打印。

至于你的运算符>>,你不应该让它成为你班级的成员。如果必须的话,可以在类声明中将其声明为友元,但将函数本身写在外面。

Shouldn't I be able to use a variable
of the type non_blank anywhere I would
use a std::string? Isn't it the
porpuse of the cast operator?

Not quite. If the compiler sees you doing something that it knows requires a std::string, it can call your conversion operator to get one. But in the case of the ostream operator <<, it doesn't have a single specific function to call, but rather quite a lot of them, all different and none matching precisely the actual type you mean to print. So it lists a whole bunch of candidates, none of which is a strong enough match. You need to define an ostream operator << for your type in order to make it print as it should.

As for your operator >>, you should make it not be a member of your class. Declare it as a friend within the class declaration if you must, but write the function itself outside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文