读取文件直到空行
阅读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?? answerWhy can't I access the private variable
string::data_
directly in the definition of theoperator >>
?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不完全是。如果编译器发现您正在做一些它知道需要 std::string 的事情,它可以调用您的转换运算符来获取它。但对于 ostream 运算符 << 来说,它没有一个特定的函数可供调用,而是有相当多的函数,它们全部不同,并且没有一个与您要打印的实际类型精确匹配。因此它列出了一大堆候选者,但没有一个是足够强大的匹配。您需要定义一个 ostream 运算符 <<适合您的类型,以便使其按应有的方式打印。
至于你的运算符>>,你不应该让它成为你班级的成员。如果必须的话,可以在类声明中将其声明为友元,但将函数本身写在外面。
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.