3

What does this error mean?

error: no matching function for call to `foo::bar(Qux (&)())`

I understand that the compiler (g++) is failing to match a function call with a foo member function. I am asking specifically about the extra stuff at the end; this bit: (&)().

For reference, my function call is like this:

// inside another member function of `foo`
Qux qux();
this->bar(qux);

And the signature for bar is:

virtual void bar(Qux&);

The compiler is also saying that the only candidate is:

virtual void bar(Qux&);

How does my function call signature differ from my definition signature, and what does (&)() mean?

jds
  • 7,910
  • 11
  • 63
  • 101
  • possible duplicate of [Default constructor with empty brackets](http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets) – Matthieu M. May 07 '14 at 12:07
  • @MatthieuM., I'm new to programming and SO, but I have found that the community tends to say questions are duplicates because to the answerers there is a connection between question X and question Y. But often the person learning about and struggling with X doesn't know that part of understanding X is understanding Y. I think that a better solution/comment would offer related and suggested reading over an opaque "duplicate"; without the answers below, I might not even understand why the question could be seen as a duplicate. Anyway, thanks for your help and sorry for the soapbox. – jds May 07 '14 at 12:25
  • 2
    I agree it's confusing, I wish that one day we'll have a "close: already answered here" instead. It's not a judgement of value on your question, or anything, it's just a way to redirect all discussions to the same page so that a would-be learner can get everything at once instead of having to check every single potential page. – Matthieu M. May 07 '14 at 14:25

5 Answers5

7

Well, (&)() alone doesn't mean anything. (&)() is just a part of Qux (&)() which means reference to a function which takes nothing and return Qux. And this arises because of this:

Qux qux();      //PROBLEM!
this->bar(qux);

The first line here does NOT declare an object. It declares a function instead.

Search for vexing parse in C++, on this site, you'll see lots of topic on it, that discuss this problem in detail.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

You should use Qux qux; (or alternatively in C++11 Qux qux{};) to avoid the most vexing parse.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 3
    It is not *"most vexing parse"*. It is just "vexing parse". Most vexing parse causes here => `T x(U());`. – Nawaz May 07 '14 at 11:59
  • 1
    @nawaz [Wikipedia](http://en.wikipedia.org/wiki/Most_vexing_parse) and [Google](https://www.google.co.uk/search?q=%22vexing+parse%22&oq=%22vexing+parse%22&aqs=chrome..69i57j0l4.1792j0j7&sourceid=chrome&es_sm=0&ie=UTF-8) disagree with you. – JBentley May 07 '14 at 12:02
  • 1
    I agree with @Nawas. However, from there on it's arguable (both ways) what is really the most vexing parse. Scott Meyers used a very restricted initial example, and some folks say that that's the only true most vexing parse, while e.g. I say let's be practical about terminology. It is after all ours to choose. – Cheers and hth. - Alf May 07 '14 at 12:02
  • @Nawaz: I wasn't aware that vexation could be objectively quantified. How do you determine that `T x(U());` is more vexing, and that nothing is more vexing than that? – Mike Seymour May 07 '14 at 12:03
  • @JBentley: Please look at [the link](http://en.wikipedia.org/wiki/Most_vexing_parse) again you provided. I don't think it disagree with me! It gives example of `TimeKeeper time_keeper(Timer());` which is same as `T x(U());` (which I gave!). – Nawaz May 07 '14 at 12:04
  • @MikeSeymour: I didn't quantify. But it seems `T x(U())` is more complex to decipher than `T x();` Hence the terminology "most". – Nawaz May 07 '14 at 12:07
  • 2
    @Nawaz I recently asked Scott over email if he considered this the most vexing parse and he said yes he does. Since he coined the term, I would consider that the authoritative answer. – Shafik Yaghmour May 07 '14 at 12:07
  • @ShafikYaghmour: I disagree with Scott, then ;-) – Nawaz May 07 '14 at 12:09
  • @Nawaz Most vexing parse refers to the fact that `Foo foo();` is parsed as a function signature, despite apparent ambiguity to the reader. It doesn't matter what example you use. In fact Meyers himself gives this example in the book where he coined the phrase: `Widget w();`. The "most vexing" refers to the C++ rule itself, not to the most complicated example you can think up. – JBentley May 07 '14 at 12:09
  • @JBentley: Hmm. I thought you were saying Wikipedia disgaree with me? Well, it doesn't. If you use "most" for `T x()`, then you probably have to use "mostest" for `T x(U())`? – Nawaz May 07 '14 at 12:11
  • @Nawaz That is because you are choosing to interpret "most vexing" as referring to that specific example, which is just the example Wikipedia chose to give. In fact, neither one is "most" or "least" vexing, because they are both examples of exactly the same principle. The only difference is that in your example you're using an anonymous parameter in the function signature, but it's the same principle at work, and it's the principle itself which is vexing. – JBentley May 07 '14 at 12:14
  • @Nawaz In fact, if you read further into the article you will see that other examples are given too, and no specific one of them is claimed as "the" most vexing parse. – JBentley May 07 '14 at 12:16
  • With a quick search, I don't find reference to *vexing parse*, and all examples I found for *most vexing parse* use `T x(U())`... – Jarod42 May 07 '14 at 12:17
  • @JBentley: I don't see *other examples* in the article (please don't post comment without reading the article). BTW, I think "complexity" determines whether it is "most" or not. `T x(U())` looks more complex to decipher, and I think it is appropriate to qualify it with "most" vexing parse! – Nawaz May 07 '14 at 12:19
  • @Nawaz Then you did not read the entire article: **"An even simpler example appears when a functional cast is intended to convert an expression for initializing a variable or passing to a constructor parameter"**. You're still confusing the issue. It's not the example which is the "most vexing", it is the C++ rule which is "most vexing". – JBentley May 07 '14 at 12:21
  • @JBentley this is pretty persistent notion on SO, I have had a couple of conversation like this before [here](https://stackoverflow.com/questions/20529434/member-must-have-class-struct-union/20529468#comment30695580_20529468) and [here](http://stackoverflow.com/questions/19250608/how-is-this-possible-to-use-in-c#comment30176608_19250608). – Shafik Yaghmour May 07 '14 at 15:55
1

The expression Qux (&)() means a reference to a function taking no arguments and returning Qux. This has already been explained by others. But that does not really address the specific detail of What does (&)() mean in a [g++] C++ compiler error message?

When the compiler encounters a function call that it cannot resolve it tried to provide as much information in the error as possible. This particular type of error in g++ is printed as multiple lines, from the second on, the compiler will list the exact signatures of the candidates for overload resolution (i.e. known overloads of the function name found by lookup at the place of call). The first line is a bit trickier, as it represents the actual call: this->bar(qux). If that was printed in the error message you would have to figure out what are the types of the different arguments, in particular what is the type of this and the type of qux.

The approach taken by the g++ implementation is to write something like a function signature that somehow represents the call. The problem is that it is actually impossible to represent the least restrictive signature that could be used, so it uses the same syntax with a slightly different meaning.

In particular, an lvalue-expression argument will be denoted by a (possibly const) reference to the type of the expression, while an rvalue-expression is denoted by a non-reference type. Going back to the error message you got:

error: no matching function for call to `foo::bar(Qux (&)())`

This means that at the place of call, the first argument is an lvalue-expression denoting an object (well, functions are not objects but bear with me) of type Qux(), that is a function taking no arguments and returning a Qux by value.

In this particular case, the argument being a non-const lvalue-expression, the reading is not more confusing than parsing the type itself, but in other cases it is important to note that this is not really a signature. For example if the error message was:

error: no matching function for call to `foo::bar(Qux)`

It could be non-obvious why the only candidate is not a perfect match, does it not take a Qux, which is what you are telling me that is being passed? Except that this syntax is representing a call with an rvalue-expression that cannot be bound by an lvalue-reference.

Similarly, if the argument was a const Qux lvalue, the error would have been:

error: no matching function for call to `foo::bar(const Qux&)`

Indicating that the argument is an lvalue-expression but that the type is const Qux which again cannot be bound by a non-const lvalue-reference.

Reading error messages is an art in itself, but it is an important part of the craft. You should pay attention whenever you have an error at the language that the compiler is using, as that will help you understand the issue the next time that you see a similar error message.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0
Qux qux();

declares a function taking no arguments and returning Qux. You should write

Qux qux;

to define an object of type Qux.

4pie0
  • 29,204
  • 9
  • 82
  • 118
0
Qux qux();

That is a function declaration.

this->bar(qux);

That is attempting to pass a reference to the function qux to bar.

If you wanted to default-construct qux, leave out the ():

Qux qux;
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98