2017-12-02 8 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)); 

(오류 : 표준 '유형의 참조의 잘못된 초기화 :: ifstream & {aka std :: basic_ifstream &} '유형'char '식에서 |)

관련성이있는 경우 프로그램 자체는 이전에 입력 한 문자로 시작하는 단어를 건너 뛰고 파일에서 읽은 문장을 화면에 인쇄합니다 (순서가 아닌 물리적 인 끝 부분은 '#'임).

이렇게 오류가 발생하여 컴파일되지 않습니다. 그것은 매우 바보 같은 실수 일지 모르지만 나는 여전히 프로그래밍에있어 매우 새롭다. 정말 고맙습니다!

EDIT. 나는 또한 프로그램의 목적에 따라 test_word에 대한 반환이 거부되어서는 안된다는 것을 깨달았다. 나는 밤새도록 그것을 썼다. 그리고 나는 그것이 약간의 바보 같은 실수를 가지고있을 것이다라는 것을 알고 있었다. 죄송합니다!

+0

'test_word'는 두 개의 인수를 취합니다. 'print_word (mainfile, test_word (바람직하지 않은));로 당신의 호출은 하나만 제공하고 있습니다. – WhozCraig

+0

아, 이제 알겠습니다! 정말 고맙습니다 WhozCraig! – LyoTheLyon

답변

2

mainfileundesired이 무엇인지 전혀 알지 못하기에 충분한 정보가 없다고 생각합니다. 그러나 test_word은 두 개의 인수가 필요하며 하나만 전달합니다. (그러나 우리가 볼 수없는 헤더 파일에 지정된 기본 인수가있을 수 있습니다).

+0

네 말이 맞아! 내가 명확하게하기 위해 전체 코드를 대신 제공했습니다. 이것은 나의 첫 번째 게시물이었고 사용되지 않았습니다. 정말 고마워요! 문제는 실제로 하나의 인수 만 test_word에 전달하는 것입니다. – LyoTheLyon