- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 istream& getline
而不是使用 istream& operator
从文件中读取行。
结果cpp
#include "Result.h"
Result::Result()
{
Coursename[0] = '\0';
}
Result::Result( const char * nam, unsigned scor )
{
strncpy( Coursename, nam, CourseNameSiz );
score = scor;
}
//istream& getline( istream & input, Result & Re, ',' )
istream & operator >>( istream & input, Result & Re )
{
//getline(Re.Coursename, input, ',');
input >> Re.Coursename >> Re.score;
return input;
}
ostream & operator <<( ostream & os, const Result & Re )
{
os << " Unit Name: " << Re.Coursename << '\n'
<< " Result: " << Re.score << '\n';
return os;
}
int Result::GetScore() const
{
return score;
}
inline void Result::SetScore( unsigned scor )
{
score = scor;
}
结果标题
#ifndef RESULT_H
#define RESULT_H
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
const unsigned CourseNameSiz = 10;
class Result
{
public:
Result();
Result( const char * nam, unsigned scor );
int GetScore() const;
// Get the number of s.
void SetScore( unsigned scor );
// Set the number of score.
//string Result:GetCourseName() const;
friend ostream & operator <<( ostream & os, const Result & Re );
//friend istream & getline( istream & input, Result & Re, ',' );
friend istream & operator >>( istream & input, Result & Re );
private:
char Coursename[CourseNameSiz];
//string Coursename[CourseNameSiz]; // Unit name, C style string
int score; // number of scores
string str;
};
#endif // RESULT_H
这一切都按照我想要的方式工作,但我需要更改它,而不是一次读一个词,我想使用带有分隔符的 istream& getline。代码中的大部分注释是我尝试更改为 getline 方法。我制作了一个字符串数组,作为测试,我试图将读取文件中的第一项放入其中,但显然没有用。
我只是想在这里寻找一点指导。提前致谢
编辑:输入文件:rinput.txt
31525 1 4
JPN_101 3 Japanese, 90,
ICT 4 example1, 91,
TIC 4 example2, 92,
CIT 4 example3, 93
所以在上面写着 example1 和 91 的地方,这是我尝试使用带有定界符的 getline 的两个项目,因为我想在我的读取文件中使用空格
其他信息在不同的类中读取,当我让这个类工作时,我将更改为 getline 方法。
输出文件:routput.txt
Student ID: 31525
Semester: 1
Unit: JPN_101
Credits: 3
Unit Name: Japanese
Result: 90
Unit: ICT
Credits: 4
Unit Name: example1
Result: 91
Unit: TIC
Credits: 4
Unit Name: example2
Result: 92
Unit: CIT
Credits: 4
Unit Name: example3
Result: 93
Number of courses = 4
Total credits = 15
这是我对数组字符使用运算符方法时的输出。
include\Result.h|28|error: expected identifier before ','|
include\Result.h|28|error: expected ',' or '...' before ','|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
它指向头文件第 28 行,它是 friend istream & getline( istream & input, Result & Re, ',' );
我相信我只是不明白为什么只是将其更改为字符串数组 (string Coursename[CourseNameSiz];
) 然后将 operator
方法更改为 getline
方法不像操作符方法那样只是让它返回输入。
我希望我的解释是正确的,我请人稍微翻译一下以帮助我更好地表述。
谢谢
最佳答案
这一行
friend istream & getline( istream & input, Result & Re, ',' );
不会编译,因为你有一个常量作为第三个参数而不是参数声明。
在您的情况下,因为您在方法内对 getline 的调用中对 ','
进行了硬编码,所以根本不需要传入逗号。
friend istream & getline( istream & input, Result & Re);
同样,当你定义函数时,你不需要函数定义中的','
:
istream& getline( istream & input, Result & Re)
{
getline(Re.Coursename, input, ',');
return input;
}
请注意,您仍然需要在 getline
的内部调用中使用逗号。
这应该可以修复您列出的编译器错误,如果它不起作用,您仍然需要调试代码。
关于c++ - 将 istream 运算符 >> 转换为 istream getline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508337/
main = do input [f a] -> f [a] sequenceA [] = pure [] sequenceA (x:xs) = (:) x sequenceA xs 很
while(getline()) 和 while(!getline().eof()) 有什么区别? 正在解析输入字符串。我尝试了两种条件检查,结果有所不同。 std::string testStr =
我知道我知道。以前有人问过这个问题,但我查看了所有答案,但似乎没有一个能解决我的问题。当我使用 getline 函数获取文件中一行的内容时,它不起作用。 getline(file, line); '文
使用 getline() 函数时出现以下错误: 没有重载函数“getline”的实例匹配参数列表 在一个名为“Time”的类中,我在读取以下输入时使用它: istream & operator >>
我正在阅读 C++ 入门书并尝试所有代码示例。我对这个很感兴趣: #include #include using std::string; using std::cin; using std::c
#include #include #include using namespace std; int main() { char d,a[9],e[9]; cin.getline(a,9); c
假设我想从控制台读取一个整数,并且我不希望程序在输入非整数字符时中断。这就是我的做法: #include #include #include using namespace std; int m
这可能不是一个错误,但我不知道出了什么问题。我的第一个条目在第二次迭代中对 str1 重复,并且从那时起也是如此。只有第一次迭代顺利。 #include #include using namesp
string text; getline(text.c_str(),256); 1) 我收到错误消息“错误:没有匹配函数来调用‘getline(const char*, int)”上面有什么问题,因为
哪个更好,更受欢迎?我真的发现阅读 API 令人困惑。 最佳答案 成员(member)版读入char*,免费版读入std::string。所以更喜欢免费版!像这样使用它: std::istream &
我正在尝试解决这个问题,但出于某种原因,我一直收到这个问题: no instance of "getline" matches the argument list. 我查过这个问题,很多次是因为人们使
长话短说 使用 libc++ 版本的 getline 函数的程序在从管道读取输入时会阻塞,直到管道的缓冲区已满。 NOT libstdc++ 版本的 getline 函数也是如此:这里函数立即读取并返
我的程序的一部分: #include #include #include #include #include using namespace std; /* Works for istrin
我是一名正在准备期末考试的 C++ 初学者。我用两种方式写了一个程序。第一个代码使用 cin.getline() 并且不能正常工作。第二个代码使用 cin.get() 和 cin >> 并正确执行所有
我正在尝试使用 getline 解析文档以获取整行并将其放入名为“line”的字符串变量中。问题是我收到一条错误消息:“没有重载函数 getline 的实例与参数列表匹配。”谁能帮我解决这个问题? #
我有一个带有 getline 函数的源代码文件,当我编译它时收到错误(下面的代码和错误)。我的问题是我从一个已经编译和工作的程序(也包括在下面)中复制并粘贴了整个函数。我在程序的其他 2 个源代码文件
我正在尝试从文件中读取,但 C++ 不想运行 getline()。 我收到这个错误: C:\main.cpp:18: error: no matching function for call to '
例子: std::ifstream in("some_file.txt"); std::string line; // must be outside ? while(getline(in,line)
注意:已解决,问题不是 getline() 而是 find 函数数组填充不当! 在发布我自己的问题之前,我已经查找了几个问题,但找不到我的问题的答案。这是我发布的第一个问题,但在发布我自己的问题之前,
我的代码块中有一个 getline 函数。但是在编译 make 文件时出现以下错误: cc -DMAIN -c -o terp.o terp.c terp.c:130:15: error: con
我是一名优秀的程序员,十分优秀!