2015-11-06 29 views
0

正如在主題中所述,我很難得到一個基本的文本處理器來編譯。以下代碼在基本部分中起作用。這意味着。當find_text()和match()函數被排除時,它將獲取一個文本文件並將其讀入一個Document類並使用print()輸出它。但是,當我包含上面提到的功能,並嘗試編譯它給出了大量的錯誤信息。在C++中獲取基本的文本處理器來編譯

此代碼來自Stroustrup「Programming:practice and principles using C++」第20章。在練習中,我應該獲取示例代碼部分以運行並繼續編寫match()函數。任何人都可以指出代碼中的錯誤?我正在使用Visual Studio 2013和2013年11月的CTP編譯器。是

#include <algorithm> 
#include <string> 
#include <vector> 
#include <iostream> 
#include<fstream> 
#include<sstream> 
#include <list> 

using namespace std; 

using Line = vector<char>; // a line is a vector of characters 

//....................TEXT ITERATOR CLASS...................... 

class Text_iterator { // keep track of line and character position within   a line 
    list<Line>::iterator ln; 
    Line::iterator pos; 
public: 
    // start the iterator at line ll’s character position pp: 
    Text_iterator(list<Line>::iterator ll, Line::iterator pp) 
     :ln { ll }, pos { pp } { } 

    char& operator*() { return *pos; } 

    Text_iterator& operator++(); 

    bool operator==(const Text_iterator& other) const { 
     return ln == other.ln && pos == other.pos; 
    } 
    bool operator!=(const Text_iterator& other) const { 
     return !(*this == other); 
    } 
}; 

Text_iterator& Text_iterator::operator++() { 
    ++pos; // proceed to next character 
    if (pos == (*ln).end()) { 
     ++ln; // proceed to next line 
     pos = (*ln).begin(); // bad if ln==line.end(); so make sure it isn’t 
    } 
    return *this; 
} 

//....................DOCUMENT CLASS...................... 

struct Document { 
    list<Line> line;   //doucument is a list of lines 
    Document() { line.push_back(Line { }); } 


    Text_iterator begin() // first character of first line 
    { 
     return Text_iterator(line.begin(), (*line.begin()).begin()); 
    } 
    Text_iterator end() // one beyond the last character of the last line 
    { 
     auto last = line.end(); 
     --last; // we know that the document is not empty 
     return Text_iterator(last, (*last).end()); 
    } 
}; 

istream& operator>>(istream& is, Document& d) { 
    for (char ch; is.get(ch);) { 
     d.line.back().push_back(ch); // add the character 
     if (ch == '\n') 
      d.line.push_back(Line { }); // add another line 
    } 
    if (d.line.back().size()) d.line.push_back(Line { }); // add final empty line 
    return is; 
} 

//....................HELPER FUNCTIONS...................... 

void erase_line(Document& d, int n) { 
    if (n < 0 || d.line.size() - 1 <= n) return; 
    auto p = d.line.begin(); 
    advance(p, n); 
    d.line.erase(p); 
} 

void print(Document& d) { 
    for (auto p : d) cout << p; 
    // int the book sample code is different: "cout<<*p"` but the book code gives error of illegal transition. This would also need a comment 
} 

/* 
bool match(Text_iterator first, Text_iterator last, const string& s) { 

    for (int i = 0; i < s.size(); ++i) { 
     if (*first == s[i]); 
     if (*first != s[i])return false; 
    } 
    return true; 
} 

Text_iterator find_txt(Text_iterator first, Text_iterator last, const string& s) { 
    if (s.size() == 0) return last; // can’t find an empty string 
    char first_char = s[0]; 
    while (true) { 
     auto p = find(first, last, first_char); 
     if (p == last || match(p, last, s)) return p; 
     first = ++p; // look at the next character 
    } 
} 
*/ 

int main() { 

    string filename1 = "myDoc.txt"; 
    ifstream stream { filename1 }; 

    Document text1; 

    stream >> text1; 

    print(text1); 

    char ch; cin >> ch; 
} 

我得到的錯誤如下:

Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 text processing proov 
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 text processing proov 
Error 16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1 text processing proov 
Error 21 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1 text processing proov 
Error 26 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1 text processing proov 
Error 13 error C2868: 'std::iterator_traits<_InIt>::value_type' : illegal syntax for using-declaration; expected qualified-name c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 text processing proov 
Error 28 error C2868: 'std::iterator_traits<_InIt>::reference' : illegal syntax for using-declaration; expected qualified-name c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1 text processing proov 
Error 23 error C2868: 'std::iterator_traits<_InIt>::pointer' : illegal syntax for using-declaration; expected qualified-name c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1 text processing proov 
Error 8 error C2868: 'std::iterator_traits<_InIt>::iterator_category' : illegal syntax for using-declaration; expected qualified-name c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 text processing proov 
Error 18 error C2868: 'std::iterator_traits<_InIt>::difference_type' : illegal syntax for using-declaration; expected qualified-name c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1 text processing proov 
Error 29 error C2665: 'std::_Debug_range2' : none of the 2 overloads could convert all the argument types c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 584 1 text processing proov 
Error 12 error C2602: 'std::iterator_traits<_InIt>::value_type' is not a member of a base class of 'std::iterator_traits<_InIt>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 text processing proov 
Error 27 error C2602: 'std::iterator_traits<_InIt>::reference' is not a member of a base class of 'std::iterator_traits<_InIt>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1 text processing proov 
Error 22 error C2602: 'std::iterator_traits<_InIt>::pointer' is not a member of a base class of 'std::iterator_traits<_InIt>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1 text processing proov 
Error 7 error C2602: 'std::iterator_traits<_InIt>::iterator_category' is not a member of a base class of 'std::iterator_traits<_InIt>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 text processing proov 
Error 17 error C2602: 'std::iterator_traits<_InIt>::difference_type' is not a member of a base class of 'std::iterator_traits<_InIt>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1 text processing proov 
Error 10 error C2146: syntax error : missing ';' before identifier 'value_type' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 text processing proov 
Error 25 error C2146: syntax error : missing ';' before identifier 'reference' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1 text processing proov 
Error 20 error C2146: syntax error : missing ';' before identifier 'pointer' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1 text processing proov 
Error 5 error C2146: syntax error : missing ';' before identifier 'iterator_category' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 text processing proov 
Error 15 error C2146: syntax error : missing ';' before identifier 'difference_type' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1 text processing proov 
Error 9 error C2039: 'value_type' : is not a member of 'Text_iterator' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1 text processing proov 
Error 24 error C2039: 'reference' : is not a member of 'Text_iterator' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1 text processing proov 
Error 19 error C2039: 'pointer' : is not a member of 'Text_iterator' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1 text processing proov 
Error 4 error C2039: 'iterator_category' : is not a member of 'Text_iterator' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1 text processing proov 
Error 14 error C2039: 'difference_type' : is not a member of 'Text_iterator' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1 text processing proov 

回答

3

當你在標準庫中調用函數時,使用自定義的迭代器,他們有一定的預期。

我看到的錯誤:

'value_type' : is not a member of 'Text_iterator' 
'reference' : is not a member of 'Text_iterator' 
'pointer' : is not a member of 'Text_iterator' 
'iterator_category' : is not a member of 'Text_iterator' 
'difference_type' : is not a member of 'Text_iterator' 

看看在http://en.cppreference.com/w/cpp/iterator/iterator明白什麼是預期迭代器。您可以通過從std::iterator派生Text_iterator來修復編譯器問題。

class Text_iterator : public std::iterator<std::input_iterator_tag, char> { 
    ... 
}; 
+0

謝謝你現在的作品,我可以繼續寫功能。據我所知,問題是在Text_iterator類的聲明語法中,而不是該類不符合要求。我複製了這本書的內容,並假定他們未經修改就開始工作,並假定我在其他地方犯了一個錯誤。仍然存在一個問題。如果我使用print()函數的書中的代碼,我會得到非法的間接錯誤。如果刪除解引用操作符,代碼將起作用。那裏有什麼問題。 – fredric