gpt4 book ai didi

c++ - 作为引用传递时 istream 的无效初始化错误

转载 作者:行者123 更新时间:2023-11-28 01:40:00 25 4
gpt4 key购买 nike

我目前正在努力解决与 ifstreams 相关的编译器错误,非常感谢任何帮助!提前致谢!

我的代码如下:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const char EOS = '#';

bool end_of_sequence (ifstream &file)
{
//Pre: EOS has not been read.
//Post: whether or not the next character to read is EOS, in which case it is read.
char test = file.get();
bool ii_EOS = (test == EOS);
if (!ii_EOS) file.unget();
return ii_EOS;
}

bool is_it_letter (char value_to_test)
{
//Pre: value_to_test is an English character.
//Post: whether or not value_to_test is a letter.
return ((value_to_test >= 'a' && value_to_test <= 'z') || (value_to_test >= 'A' && value_to_test <= 'Z') || value_to_test == '·');
}

bool test_word (ifstream &file, char undesired)
{
//Pre: file hasn't reached eof and is ready for a word to be read.
//Post: whether or not [not (the first letter of the next word is undesired)].
char first_letter = file.get();
bool test = (first_letter != undesired && is_it_letter(first_letter));
file.unget();
return (!test); //I later found out test shouldn't be denied.
}

void print_spaces (ifstream &file)
{
//Pre: true.
//Post: has read all the next non-letters in file and printed them onscreen.
char current_space = ' ';
while (!is_it_letter(current_space) && !end_of_sequence(file))
{
file.get(current_space);
if (!is_it_letter(current_space) && current_space != EOS) cout << current_space;
}
file.unget();
}

void print_word (ifstream &file, bool printable)
{
//Pre: true.
//Post: if file has a word ready to be read, it is read. It is also printed onscreen if printable is true.
char current_letter = file.get();
while (is_it_letter(current_letter))
{
if (printable) cout << current_letter;
file.get(current_letter);
}
file.unget();
}

int main ()
{
//Declarations.
string input;
char undesired;

//Input.
cout << "INTRODUEIX EL NOM DEL FITXER:" << endl;
cin >> input;
ifstream mainfile(input.c_str());
if (!mainfile.is_open())
{
cout << "NO HEM TROBAT EL FITXER." << endl;
return 1;
}
cout << "INTRODUEIX UN CARACTER:" << endl;
cin >> undesired;

//Comput + output.
cout << "TEXT RESULTANT D'ELIMINAR LES PARAULES QUE COMENCEN PER " << undesired << ":" << endl;
while (!end_of_sequence(mainfile))
{
print_word(mainfile, test_word(mainfile,undesired));
print_spaces(mainfile);
}

return 0;
}

我遇到此错误的函数调用是:

print_word(mainfile, test_word(undesired));

(错误:从类型为“char”的表达式中对类型为“std::ifstream& {aka std::basic_ifstream&}”的引用的初始化无效|)

在相关的情况下,程序本身旨在在屏幕上打印从文件中读取的句子(序列的非物理结尾,即“#”)跳过以先前输入的字母开头的单词。

由于上述错误,它无法编译。这可能是一个非常愚蠢的错误,我在编程方面仍然很新。非常感谢!

编辑。我还意识到(在我运行它之后)根据程序的目的不应该拒绝返回 test_word。我整个晚上都写完了,我知道它会有一些愚蠢的错误。对不起!

最佳答案

我认为没有足够的信息可以确定,因为我不知道mainfileundesired 是什么。但是,test_word 需要两个参数,而您只传递了一个。 (然而,在我们看不到的头文件中可能指定了默认参数)。

关于c++ - 作为引用传递时 istream 的无效初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47603476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com