What does 'illegal indirection' mean?

L

Lambda

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 into the
termBuffer vector.
But I got an error: 'error C2100: illegal indirection'
What does it mean?

BTW, as I include <vector> in the header file,
the cpp file include the header, need I add '#include <vector>' the
cpp file?

Best Regards,
Lambda
 
L

Lambda

Which vector does it copy?



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.




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

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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top