-3

The following code:

#include <iostream>

using namespace std;

class Myclass
{
private:
    Myclass ();

public:
    int num;
};

int main()
{
    Myclass foo();

    return 0;
}

Compiles without any warnings or errors in Eclipse.

However

#include <iostream>

using namespace std;

class Myclass
{
private:
    Myclass ();

public:
    int num;
};

int main()
{
    Myclass foo;

    return 0;
}

Gives me this error: error: 'Myclass::Myclass()' is private within this context

Why does foo give me this error while foo() doesn't? Is foo() being mistaken as a function or does date_type name() have a special meaning?

tempdev nova
  • 211
  • 1
  • 8
  • 2
    did you confuse the working with the non working code? I'd expect an error for the first and none (perhaps warnings) for the second – 463035818_is_not_an_ai Jul 18 '22 at 12:04
  • actually yes. In the text you write about `foo()` having no error while `foo` is an error. The code is swapped – 463035818_is_not_an_ai Jul 18 '22 at 12:05
  • 1
    *Is foo() being mistaken as a function* Yes. Suppose you declare a function called `foo` with no arguments that returns a `Myclass`, that would be `Myclass foo();` – 463035818_is_not_an_ai Jul 18 '22 at 12:06
  • So `MyClass foo()` in main is being mistaken as a function decleration in `main()`? – tempdev nova Jul 18 '22 at 12:11
  • 1
    It's not mistaken, it's what *it is*. – StoryTeller - Unslander Monica Jul 18 '22 at 12:11
  • this is not the most vexing parse. – 463035818_is_not_an_ai Jul 18 '22 at 12:24
  • ...nevertheless the answers on the duplicates should help to explain. – 463035818_is_not_an_ai Jul 18 '22 at 12:27
  • @463035818_is_not_a_number It is still "vexing parse" but not the "most vexing parse". See [Is most vexing parse a formally defined concept](https://stackoverflow.com/questions/71937565/is-most-vexing-parse-a-formally-defined-concept). Also, most vexing parse is not a formally defined concept. So saying whether it is or not is pointless. And as you said, the dupes should help OP understand the problem nevertheless. – Jason Jul 18 '22 at 12:28
  • *Is `foo()` being mistaken as a function...* You are mistaking the function declaration as being a local variable. – Eljay Jul 18 '22 at 12:28
  • @AnoopRana that is strange logic. I dont need a formal definition to know if something is an apple or not when I see an apple. The term was coined by Scott in his books and there he explains what the term stands for. It does not stand for the issue in OPs code – 463035818_is_not_an_ai Jul 18 '22 at 12:29
  • @463035818_is_not_a_number What i mean is that the label "most vexing parse" isn't formally defined by the C++ standard and that the declaration is a function declaration and not a variable declaration. Moreover, IMO `int a();` is still most vexing parse. But seeing [this](https://stackoverflow.com/questions/71937565/is-most-vexing-parse-a-formally-defined-concept), people seem to disagree. But there is no right answer here. You can call it whatever you want. `int a();` is a function declaration and it can sometimes be incorrectly taken as a variable declaration by beginners. – Jason Jul 18 '22 at 12:31
  • @AnoopRana so what? I dont get what you are trying to say. `Myclass foo();` is not the most vexing parse. Do you disagree? – 463035818_is_not_an_ai Jul 18 '22 at 12:33
  • @463035818_is_not_a_number Yes, i disagree. In particular `Myclass foo();` is indeed most vexing parse. See [my old comments at this thread](https://stackoverflow.com/questions/71937565/is-most-vexing-parse-a-formally-defined-concept#comment127122131_71939741) where i said the same. – Jason Jul 18 '22 at 12:35

2 Answers2

6

Why can MyClass foo() access private default constructor?

It cannot (fancy hacks that allow you to call a private constructor aside).

Is foo() being mistaken as a function

Myclass foo(); is a function declaration. The mistake is to expect it to be something else. To call the constructor write:

Myclass foo;  // or
Myclass foo{}; 

As this mistake is rather common, compilers typically warn about it. Don't ignore warnings, and crank up the warning level when there was no message for your code.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Could've closed this as dupe. There are plenty of dupes for this. – Jason Jul 18 '22 at 12:37
  • @AnoopRana I didn't find a good duplicate. It is not the most vexing parse and duplicates for this are not easy to find because typically its not easily recognizable from the question or its title. – 463035818_is_not_an_ai Jul 18 '22 at 12:43
  • @AnoopRana Scott introduced the term specifically for things like `Foo bar(moo());` ie, a parameter is present. You can decide to use the term differently, but then your notion of it is different from what Scott introduced it for. – 463035818_is_not_an_ai Jul 18 '22 at 12:45
  • Some dupes are: [dupe1](https://stackoverflow.com/questions/71806191/how-to-create-an-object-in-a-form-like-this-ifstream-in/71806283#71806283), [dupe2](https://stackoverflow.com/questions/71538476/trying-to-understand-default-constructors-and-member-initialisatioon/71538753#71538753), [dupe3](https://stackoverflow.com/questions/70115291/why-does-calling-with-empty-parenthesis-not-call-the-default-constructor).These are some of the common dupes that i could find.There are many more. I will leave this comment here as this is a common question(by beginners) so this might help close them as dupe. – Jason Jul 18 '22 at 12:49
  • My main criticism is that if most vexing parse applies to something complex like `Foo bar(moo()); ` then it naturally/logically applies to the simple/trivial case of `Foo bar();`. Just because it(`Foo a();`) is simple or trivial to one person doesn't mean that is straightforward to everybody and so doesn't disqualify it from using the term MVP. Both `Foo a(moo());` and `Foo a();` are MVP whether or not the author intended them to be. Authors can also make mistakes(or overlook things etc). – Jason Jul 18 '22 at 12:56
  • @AnoopRana I agree with your critisism but not with your conclusion. Just because the term isnt the best does not imply that it actually means something else. If you wrote the book the term would have been better defined, but you didnt – 463035818_is_not_an_ai Jul 18 '22 at 12:58
  • No writing a book doesn't make you a god. Books contain mistakes too. They are written by people for other people etc for different purposes like standardising things(terms etc). You're missing the point i am trying to make which is that the term MVP is supposed to indicate something that is potentially ambigiuous when parsing. For example, `Foo bar(moo());` but the same ambiguity exists for `Foo bar();` too! Well, maybe not for you(the author) and may not today but tomorrow it potentially can... – Jason Jul 18 '22 at 13:03
  • ...Defining a term on the assumption that `Foo bar();` is clearly a function declaration to everybody who read this statement is ignorance and an oversight by the author. – Jason Jul 18 '22 at 13:04
1

I get this: https://godbolt.org/z/4r959brh6

<source>:16:13: error: calling a private constructor of class 'Myclass'
    Myclass foo1;
            ^
<source>:8:5: note: declared private here
    Myclass ();
    ^
<source>:17:17: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
    Myclass foo2();
                ^~
<source>:17:17: note: remove parentheses to declare a variable
    Myclass foo2();
                ^~
<source>:18:13: error: calling a private constructor of class 'Myclass'
    Myclass foo3{};
            ^
<source>:8:5: note: declared private here
    Myclass ();
    ^
1 warning and 2 errors generated.
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • Both GCC and clang warn about this by default, even without `-Wall` or `-W`. https://godbolt.org/z/evzPY6G9r . Although for GCC, that warning didn't exist in GCC10 or earlier, even at `-Wall -Wextra`. But clang has had it for ages, since at least 3.4 https://godbolt.org/z/nnf97Mqo5 – Peter Cordes Aug 02 '22 at 09:17