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

时间:2017-12-02 00:54:25

标签: c++ ifstream

我目前正在努力解决与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&amp; {aka std :: basic_ifstream&amp;}'类型的引用无效

如果它是相关的,程序本身意味着在屏幕上打印从文件读取的句子(具有非物理序列的结尾,即它'#')跳过以前输入的字母开头的单词。 / p>

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

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

1 个答案:

答案 0 :(得分:2)

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