#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include "Document.h"
std::istream &operator>>(std::istream &is, Document &d)
{
char ch = ' ';
for ( ; 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;
}
Text_iterator &Text_iterator::operator++()
{
++pos; // proceed to the next character
if (pos == (*ln).end())
{
++ln; // proceed to the next line
pos = (*ln).begin(); // bad if ln == line.end(); so make sure it isn't
}
return *this;
}
Text_iterator Text_iterator::find_txt(Text_iterator first, Text_iterator last, const std::string &s)
{
if (s.size() == 0)
{
return last;
}
char first_char = s[0];
while (true)
{
auto p = std::find(first, last, first_char);
if (p == last || match(p, last, s))
{
return p;
}
first = ++p;
}
}
bool Text_iterator::match(Text_iterator first, Text_iterator last, const std::string &str)
{
if (str.empty())
{
returnfalse;
}
for (char i = 0, j = *first; i < str.length(), j != *last; ++i, ++j)
{
if (i != j)
{
returnfalse;
}
}
returntrue;
}
I have 22 compiler errors with this. The first one is:
struct Document {
list<Line> 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());
}
};
I just copied the code from the book. And I'm pretty sure that's a std::vector<char>::begin().
Here are all of the errors:
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(14): error C3646: 'begin': unknown override specifier
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(14): error C2059: syntax error: '('
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(14): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(15): error C3646: 'end': unknown override specifier
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(15): error C2059: syntax error: '('
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(16): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(16): error C2228: left of '.push_back' must have class/struct/union
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(17): error C2039: 'begin': is not a member of 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(11): note: see declaration of 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(17): error C2512: 'Text_iterator': no appropriate default constructor available
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(25): note: see declaration of 'Text_iterator'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(18): error C2039: 'end': is not a member of 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(11): note: see declaration of 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(18): error C2660: 'Text_iterator::find_txt': function does not take 2 arguments
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(19): error C3536: 'matched': cannot be used before it is initialized
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(19): error C2039: 'end': is not a member of 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(11): note: see declaration of 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(28): error C3312: no callable 'begin' function found for type 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(28): error C3312: no callable 'end' function found for type 'Document'
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(29): error C2065: 'p': undeclared identifier
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(35): warning C4018: '>=': signed/unsigned mismatch
1>Document.cpp
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(14): error C3646: 'begin': unknown override specifier
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(14): error C2059: syntax error: '('
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(14): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(15): error C3646: 'end': unknown override specifier
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(15): error C2059: syntax error: '('
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.h(16): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
It's skipping the function body because it can't recognize what "begin" is and is saying that the open paren there is a syntax error. I need to fix this error before it stops ignoring the function body.
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(16): error C2228: left of '.push_back' must have class/struct/union
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\main.cpp(35): warning C4018: '>=': signed/unsigned mismatch
1>Document.cpp
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.cpp(62): warning C4018: '<': signed/unsigned mismatch
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(984): error C2794: 'iterator_category': is not a member of any direct or indirect base class of 'std::iterator_traits<_InIt>'
1> with
1> [
1> _InIt=Text_iterator
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(3133): note: see reference to function template instantiation 'void std::_Debug_range<_InIt>(_InIt,_InIt,std::_Dbfile_t,std::_Dbline_t)' being compiled
1> with
1> [
1> _InIt=Text_iterator
1> ]
1>c:\users\osman\programming\visual_studio_2017\projects\programming_principles_practice_using_c++\document\document\document.cpp(46): note: see reference to function template instantiation '_InIt std::find<Text_iterator,char>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=Text_iterator,
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(984): error C2938: '_Iter_cat_t<Text_iterator>' : Failed to specialize alias template
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility(984): error C2062: type 'unknown-type' unexpected
Also include some harmless warnings, but please ignore those.
Okay, now I have a Segfault being trigged in the find_txt function because of the match function. I'm having trouble stepping through the code in GDB without triggering the Segfault, the and VS debugger can't download the needed symbols right now. So I'm kind of stuck.
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include "Document.h"
std::istream &operator>>(std::istream &is, Document &d)
{
char ch = ' ';
for ( ; 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;
}
Document::Text_iterator &Document::Text_iterator::operator++()
{
++pos; // proceed to the next character
if (pos == (*ln).end())
{
++ln; // proceed to the next line
pos = (*ln).begin(); // bad if ln == line.end(); so make sure it isn't
}
return *this;
}
Document::Text_iterator Document::Text_iterator::operator++(int n)
{
auto ret = *this;
pos += n;
if (pos == (*ln).end())
{
++ln;
pos = (*ln).begin();
}
ret = *this;
return ret;
}
Document::Text_iterator Document::Text_iterator::find_txt(Document::Text_iterator first, Document::Text_iterator last, const std::string &s)
{
if (s.size() == 0)
{
return last;
}
char first_char = s[0];
while (true)
{
auto p = std::find(first, last, first_char);
if (p == last || match(p, last, s))
{
return p;
}
first = ++p;
}
}
bool Document::Text_iterator::match(Document::Text_iterator first, Document::Text_iterator last, const std::string &str)
{
if (str.empty())
{
returnfalse;
}
for (char i = 0, j = *first; i < str.length() && j != *last; ++i, ++j)
{
if (i && j)
{
if (i != j)
{
returnfalse;
}
}
}
returntrue;
}
Shouldn't that for loop fill the vector up? And I'm pretty sure emplace_back expects at least one argument (which I think should be a char in this case).
Edit: Also, I changed Document::Text_iterator::match to this to get rid of the vector iterator "non-dereference-able" error:
bool Document::Text_iterator::match(Document::Text_iterator first, Document::Text_iterator last, const std::string &str)
{
if (str.empty())
{
returnfalse;
}
std::string::size_type i = 0;
Text_iterator doc_it = first;
for (; doc_it != last; ++doc_it)
{
if(i < str.size())
{
if(str[i] != *doc_it)
break;
}
elsebreak;
}
return (i == str.size()) && (doc_it == last); //Note && (doc_it == last) means full match
// if you want to allow partial match remove that part
}
It's saying it about the list iterator again after I made that change in main. I'll make that suggested change in match first and try it again. Thanks.
Edit: Just tried it. Another debug assertion failure; "vector iterator not dereferenceable", in the <vector> header on line 73. Last item on the call stack from my own code on the call stack is the Text_iterator operator* called after find_txt called std::find. And in the debugger, the code breaks on line 17 in this code (stdthrow.cpp):