2017-12-02 16 views
0

現在、ifstreamsに関連するコンパイラエラーが発生しています。前もって感謝します!リファレンスとして渡したときにistreamの無効な初期化のエラーが発生しました

私のコードは次のとおりです:

#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)); 

(エラー:型「STDの参照の無効な初期化:: ifstream & {aka std :: basic_ifstream &} 'タイプ' char '式から|)

関連する場合、プログラムそのものは以前に入力された文字で始まる単語をスキップして、ファイルから読み込まれた文章(シーケンスの物理的でない部分は '#')をスクリーンに表示します。

このようなエラーのため、コンパイルできません。それは非常にばかな間違いかもしれません、私はプログラミングでまだ非常に新しいです。どうもありがとうございます!

EDIT。私はまた、(私はそれを実行した後に)プログラムの目的に応じてtest_wordの返信が拒否されるべきではないことを認識しました。私は一晩中それを書きました、そして、私はそれがいくつかの間違ったミスをしていることを知っていました。ごめんなさい!

+0

'test_word'は2つの引数をとります。 'print_word(mainfile、test_word(望ましくない));であなたの呼び出しは1つだけを提供しています。 – WhozCraig

+0

ああ、私はそれを今見ます! WhozCraigさん、本当にありがとう! – LyoTheLyon

答えて

2

mainfileundesiredが何であるかわからないので、十分な情報がないと思います。しかし、test_wordは2つの引数を必要とし、1つだけを渡しています。 (ただし、表示されていないヘッダーファイルには、デフォルトの引数が指定されている可能性があります)。

+0

あなたはそうです!私はより明確にするために完全なコードを提供しました。これは私の最初の投稿でした、私は使用されていません、ありがとう!問題は実際にtest_wordに1つの引数を渡すことにありました。 – LyoTheLyon

関連する問題