Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Request for member 'mX' in 'w', which is of non-class type 'Widget()'

 

Compile error.

 

 

 

 

 

Full error message

 

MyMain.cpp:6: error: request for member 'mX' in 'w', which is of non-class type 'Widget()'

 

 

 

 

 

Cause

 

IDE: Qt Creator 1.3.1

Project type: Qt4 Console Application

Selected required modules: QtCore

Compiler: G++ 4.4.1

 

The following code caused the error:

 

struct Widget{ int mX; };

int main()
{
  Widget w();
  w.mX = 3;
}

 

 

 

 

 

Solution

 

Instanciate the default-constructed class without brackets:

 

struct Widget{ int mX; };

int main()
{
  Widget w;
  w.mX = 3;
}

 

 

 

 

 

(Advanced) What caused this compile error exactly?

 

From [1]: 'The code does not declare a Widget named w, it declares a function named w that takes nothing and returns a Widget'.

 

 

 

 

 

References

 

  1. Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 6: 'Containers', page 35

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict