Why can't I access a member of this class?

Nathan Osman

I have the following three class definitions:

class String
{
    public:
        String() {}
        String(const char *) {}
};

class ClassA
{
    public:
        ClassA(const String &) {}
};

class ClassB
{
    public:
        ClassB(const ClassA &, const String & = String()) {}
        void method() {}
};

Now suppose I want to create an instance of ClassB:

String name("test");
ClassA item(ClassB(name));

This doesn't work:

error: request for member 'method' in 'item', which is of non-class
  type 'ClassA ()(ClassB)'

What does this error mean? And what is this strange type ClassA ()(ClassB) the compiler keeps referring to?

LihO

This is called most vexing parse problem.

ClassA item(ClassB(name));

should either be:

ClassB b(name);
ClassA item(b);

or:

ClassA item( (ClassB(name)) );

Also have a look at: Most vexing parse: why doesn't A a(()); work?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I access a protected member from an instance of a base class?

From Dev

Why can't I access my objects member variable?

From Dev

Why can't I access my objects member variable?

From Dev

Why can't I pass a type member of a class as a template parameter?

From Dev

Why can't I set a member value for a class instance?

From Dev

Why can't I initialize a QThread in a member function of class?

From Dev

Why an instance of inherited class can't access to protected member of base class in different package

From Dev

Why an instance of inherited class can't access to protected member of base class in different package

From Dev

configatron a singleton? Why can't I access the configatron in my class

From Dev

Why I can't access an element of a class instance in Swift

From Dev

Why can't I access my class instance methods?

From Dev

Why i can't access protected property in the derived class

From Dev

Why can't I access methods specific to my child class?

From Dev

why can't I access the variable in my class. python

From Dev

Why I can't access my script variable from class?

From Dev

Why can't I access local class outside declaring method?

From Dev

Can I access a static member function on a class/struct which I don't have any forward declaration?

From Dev

Why can't we use nested type through class-member-access expression?

From Dev

why can't my class friend function access a protected member with namespaces?

From Dev

Why can't I access inherited protected fields in a derived constructor's member initialization list?

From Dev

Why can't I assign an int to the union member of a struct member?

From Dev

Why can't I make in-class initialized `const const std::string` a static member

From Dev

Why can't I overload C++ conversion operators outside a class, as a non-member function?

From Dev

Why can't I initialize a static class member in a common header file?

From Dev

A method can't access a member variable of the same class (C++)

From Dev

Class method can't access protected member "Error: identifier is undefined"

From Dev

How can I access a member of a class from a function via pointer?

From Dev

How can I access public member of private package class?

From Dev

How can I access public member of private package class?

Related Related

  1. 1

    Why can't I access a protected member from an instance of a base class?

  2. 2

    Why can't I access my objects member variable?

  3. 3

    Why can't I access my objects member variable?

  4. 4

    Why can't I pass a type member of a class as a template parameter?

  5. 5

    Why can't I set a member value for a class instance?

  6. 6

    Why can't I initialize a QThread in a member function of class?

  7. 7

    Why an instance of inherited class can't access to protected member of base class in different package

  8. 8

    Why an instance of inherited class can't access to protected member of base class in different package

  9. 9

    configatron a singleton? Why can't I access the configatron in my class

  10. 10

    Why I can't access an element of a class instance in Swift

  11. 11

    Why can't I access my class instance methods?

  12. 12

    Why i can't access protected property in the derived class

  13. 13

    Why can't I access methods specific to my child class?

  14. 14

    why can't I access the variable in my class. python

  15. 15

    Why I can't access my script variable from class?

  16. 16

    Why can't I access local class outside declaring method?

  17. 17

    Can I access a static member function on a class/struct which I don't have any forward declaration?

  18. 18

    Why can't we use nested type through class-member-access expression?

  19. 19

    why can't my class friend function access a protected member with namespaces?

  20. 20

    Why can't I access inherited protected fields in a derived constructor's member initialization list?

  21. 21

    Why can't I assign an int to the union member of a struct member?

  22. 22

    Why can't I make in-class initialized `const const std::string` a static member

  23. 23

    Why can't I overload C++ conversion operators outside a class, as a non-member function?

  24. 24

    Why can't I initialize a static class member in a common header file?

  25. 25

    A method can't access a member variable of the same class (C++)

  26. 26

    Class method can't access protected member "Error: identifier is undefined"

  27. 27

    How can I access a member of a class from a function via pointer?

  28. 28

    How can I access public member of private package class?

  29. 29

    How can I access public member of private package class?

HotTag

Archive