Domanda

I'm taking my first foray into C++, specifically the Google RE2 library, and I am stuck on some of the syntax. I am trying to invoke a function with the signature:

static bool FindAndConsumeN(StringPiece* input, const RE2& pattern,
                         const Arg* const args[], int argc);

With the code:

const re2::RE2::Arg match;
bool isMatched = RE2::FindAndConsumeN(&inputPiece, *expression,new const re2::RE2::Arg[] { &match },0)

However I am getting the compiler error:

Error   3   error C2664: 're2::RE2::FindAndConsumeN' : cannot convert parameter 3 from 'const re2::RE2::Arg (*)[]' to 'const re2::RE2::Arg *const []'

I've clearly got the datatype of the third argument wrong, but does anyone know what the correct data type is?

I am compiling the code with Visual Studio 2010

È stato utile?

Soluzione

You should use code like this:

re2::RE2::Arg match;
re2::RE2::Arg* args[] = { &match };
re2::RE2::FindAndConsumeN(NULL, pattern, args, 1);

args will be converted to const Arg* args[].

The inner const has no deal with calling code, it works only within FindAndConsumeN.

Don't use new because you can't delete array later.
(with new it would be new const re2::RE2::Arg*[])

Altri suggerimenti

Problem here is that you need a pointer to constant data, not a constant pointer to data. Use an intermediate variable to store the value of the offending argument, and I think you'll be able to sort the problem.

First, note that parameter declarations have slightly different meanings when they appear as function parameters. In this case, the actual type of the third parameter is: Arg const* const*. I don't think you can use a new expression here (and if you could, who'd delete it); the new expression required would be something like new (Arg const* const [n]); which allocates an array of n uninitialized const pointers. What you need is something more along the lines of:

std::vector<Arg const*> args;
//  Fill out args with the desired data...
... , &args[0], ...

The function signature is:

static bool FindAndConsumeN(StringPiece* input, const RE2& pattern,
                            const Arg* const args[], int argc);

The 3rd argument is const Arg* const args[], this means: constant array of const pointers to type Arg.

i.e. the array is constant and each entry is also const.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top