Error creating object: request for member '..' in '..' which is of non-class type

Asked 2 years ago, Updated 2 years ago, 26 views

in the class The one who doesn't get any prints You have defined a constructor that receives one type of int factor.

However, when creating an object in the main function,

The constructor (2) that receives one parameter is fine The generator (1) that does not receive any parameters displays an error! Why?

nonclass.cpp: In function ‘int main(int, const char**)’: nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’

class Foo
{
  public:
    Foo() {};
    Foo(int a) {};
    void bar() {};
};

int main()
{
  //This is fine
  Foo foo1(1);
  foo1.bar();

  // This isn't okay
  Foo foo2();
  foo2.bar();

  return 0;
}

c++

2022-09-21 18:28

1 Answers

Foo foo2(); -> Foo foo2; Change to.

The compiler does not use Foofoo2() as the constructor Consider the Foo object as a foo2() function that returns the Foo object.

The parenthesis of the base constructor is Foo* foo2 = new Foo() Use it only when creating pointer type objects together


2022-09-21 18:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.