On May 16, 10:13 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Lambda wrote:
> > The code is simple:
>
> > // Token.h
> > #ifndef TOKEN_H
> > #define TOKEN_H
>
> > #include <vector>
> > #include <string>
>
> > class Token
> > {
> > public:
> > Token() : type("word"), positionIncrement(1), startOffset(0),
> > endOffset(0) {}
> > void setPositionIncrement(const unsigned int increment)
> > {
> > positionIncrement = increment;
> > }
> > unsigned int getPositionIncrement() const
> > {
> > return positionIncrement;
> > }
> > void setTermBuffer(const std::vector<char>::size_type&,
> > const std::vector<char>::size_type&);
> > private:
> > std::vector<char> termBuffer;
> > std::vector<char> payload;
>
> > unsigned int startOffset;
> > unsigned int endOffset;
>
> > std::string type;
> > unsigned int positionIncrement;
> > };
>
> > #endif
>
> > // Token.cpp
> > #include <algorithm>
> > #include <iterator>
>
> > #include "Token.h"
>
> > using std::copy;
> > using std::back_inserter;
>
> > void Token::setTermBuffer(const std::vector<char>::size_type& b,
> > const std::vector<char>::size_type& e)
> > {
> > copy(b, e, back_inserter(termBuffer));
> > }
>
> > The setTermBuffer function is used to copy a vector
>
> Which vector does it copy?
>
> > into the
>
> > termBuffer vector.
> > But I got an error: 'error C2100: illegal indirection'
> > What does it mean?
>
> It usually means you're trying to use an object that does not support
> the operator* (dereferencing) in the context that requires it. For
> example, here the 'copy' function (template) actually does approximately
> this:
>
> // copy(a, b, blah)
> while (a != b) {
> *blah = *a; // line 12345
> ++a;
> ++blah;
> }
>
> Now, since you have 'b' and 'e' declared as 'size_type' (which is just
> an integral unsigned type, like 'unsigned' or 'size_t'), you cannot
> dereference it (apply the operator* to it, see line 12345).
>
> Now, what you probably intended is to copy the elements of some other
> vector between the indices 'b' and 'e', and since 'copy' expects
> something that implements the semantics of an iterator, you need to
> convert your indices into iterators. However, the function does not
> have the "source" vector reference or pointer. How the hell would the
> function know where to get those elements?
>
> It would be better if you defined your 'setTermBuffer' as a template
> (basically a wrapper around the 'copy' template):
>
> template<class It> void setTermBuffer(It b, It e) {
> std::copy(b, e, std::back_inserter(termBuffer));
> }
>
> When you call it, pass the *iterators* as the start and end, not indices
> because the iterators know what container they iterate, but indices are
> dumb in that sense.
>
>
>
> > BTW, as I include <vector> in the header file,
> > the cpp file include the header, need I add '#include <vector>' the
> > cpp file?
>
> The rule of thumb is that every module (and a C++ source file is one,
> and a C++ header file is one as well) that uses a symbol defined
> elsewhere, should include the necessary header. So, IMNSHO, yes, you do
> need to include <vector> in the 'cpp' file *regardless* of whether you
> include it into your other header, because if you don't, you create an
> unnecessary dependency between the header and the C++ file (on top of
> other dependencies that already exist between them).
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask
Thank you, Victor.
Yes, I intended to use iterator.
I focused on the error, didn't note i used size_type.
And I'll take your suggestion on 'include'.
|