visual在c++中获取基本的文本处理器进行编译
visual Getting basic text processor in c++ to compile
正如主题中所述,我在编译基本文本处理器时遇到了问题。以下代码分为基本部分。这意味着。当findtext()和match()函数被排除在外时,它会获得一个文本文件,并将其读取到Document类中,然后使用print()输出。但当我包含上面提到的函数并尝试编译时,会出现大量错误消息。
这段代码来自Stroustrup"编程:使用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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 373 1 text processing proov
Error 10 error C2146: syntax error : missing ';' before identifier 'value_type' c:program files (x86)microsoft visual studio 12.0vcincludexutility 372 1 text processing proov
Error 25 error C2146: syntax error : missing ';' before identifier 'reference' c:program files (x86)microsoft visual studio 12.0vcincludexutility 376 1 text processing proov
Error 20 error C2146: syntax error : missing ';' before identifier 'pointer' c:program files (x86)microsoft visual studio 12.0vcincludexutility 375 1 text processing proov
Error 5 error C2146: syntax error : missing ';' before identifier 'iterator_category' c:program files (x86)microsoft visual studio 12.0vcincludexutility 371 1 text processing proov
Error 15 error C2146: syntax error : missing ';' before identifier 'difference_type' c:program files (x86)microsoft visual studio 12.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 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.0vcincludexutility 373 1 text processing proov
当您在调用标准库中的函数时使用自定义迭代器时,他们会有一些期望。
我看到错误:
'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> {
...
};
相关文章:
- #定义c-预处理器常量..我做错了什么
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 预处理器:插入结构名称中的前一个行号
- 如何在c++中实现处理器调度模拟器
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 是否可以在多行字符串文本中使用 C/C++ 预处理器令牌
- 有没有一种方法可以使用预处理器将文本资源拉入原始字符串
- 如何使用预处理器从源文件中获取一行文本
- 我在让非常基本的命令提示符文本处理器工作时遇到问题。ofstream() 的问题
- visual在c++中获取基本的文本处理器进行编译
- 为文字处理器存储文本的更好方法是什么?