0

I hane got a class inserted into the namespace:

namespace CPGL
{
    class Application;
}

class Application
{
    public:

        Application()
        {
            ...
        }
};

When I tryed to create a pointer this class like this:

CPGL::Application application();
CPGL::Application* app = &application;

Strange and mysterious things are started to happen. Here is the compilation log:

error: cannot convert ‘CPGL::Application (*)()’ to ‘CPGL::Application*’ in initialization
CPGL::Application* app = &application;

The question is how does a link to the class turned to a pointer to the constructor function of this class and how to solve it?

  • 4
    Look up the most vexing parse. :) – Rakete1111 Jul 30 '18 at 15:52
  • 3
    In the code you show you have *two different* classes, `CPGL::Application` and `::Application`. It's important that you create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and also [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 30 '18 at 15:53

1 Answers1

3

CPGL::Application application(); is a function declaration, hence the compiler diagnostic shows CPGL::Application (*)(),

CPGL::Application application();
CPGL::Application* app = &application;

The expression &application is here a function pointer. This mistake is sometimes (incorrectly) referred to as The most vexing parse

What you probably intended was

CPGL::Application application;
CPGL::Application* app = &application;

Also note that your namespace declares a class Application but it is defined outside the namespace. This may or may not be a typo in the question.

I prefer to not describe this as a Most vexing parse because there is no syntax rule that would reasonably allow this as a call to the constructor. type name(); is a function declaration, period. A a(object of type B) is however the correct way call the constructor A::A(B b). So it is reasonable to think that A a(B()) creates an object of type B as the argument to A ctor. The compiler instead parses it as a type. This is discussed in the linked SO answer.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • The "(incorrectly)" makes me curious. According to Wikipedia [Most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse) is the term for _syntactic ambiguity resolution_ which may not match the expectation/intention of the human writer. (Though, even Wikipedia may fail.) – Scheff's Cat Jul 30 '18 at 16:02
  • @Scheff I've amended the answer a bit. – Captain Giraffe Jul 30 '18 at 16:14
  • So, you think there isn't such ambiguity for `A a()` as there isn't a resp. (concurrent) grammar rule for constructors which must be dis-ambiguated? Once, I become a pensioner I will read the C++ grammar in detail and check this out. (I add this to the list of things which I will do once I've the time...) – Scheff's Cat Jul 30 '18 at 16:15
  • @Scheff that is my thinking, yes. – Captain Giraffe Jul 30 '18 at 16:15