error: request for member ".." in ".." which is of non-class type

Understanding the "error: request for member '...' in '...' which is of non-class type"
Hey there tech enthusiasts! π
Have you ever encountered the dreaded "error: request for member '...' in '...' which is of non-class type" when working with object-oriented programming in C++? π€ Don't worry, you're not alone! This common issue often stems from a simple mistake that can easily be fixed. In this blog post, we'll dive into the root cause of this problem and provide you with easy solutions to get your code up and running smoothly. Let's get started! πͺ
The Scenario π
Imagine you have a class called Foo with two constructors - one that takes no arguments and another that takes one argument. Creating objects using the constructor that takes one argument works perfectly fine. However, when trying to create objects using the constructor with no arguments, you encounter the frustrating error. Here's an example code snippet to illustrate the issue:
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// This works...
Foo foo1(1);
foo1.bar();
// But this does not...
Foo foo2(); // Error occurs here
foo2.bar();
return 0;
}The Error π«
When you attempt to compile the code, you're met with the following error message:
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 ()()βThis error indicates that the compiler is treating the declaration of foo2 as a function rather than an object of Foo class. The syntax you used to declare foo2 inadvertently misled the compiler, causing it to think it's a function returning a Foo object.
The Explanation β
The culprit behind this error is actually the usage of parentheses () when declaring objects. In C++, objects are created using constructors without any parentheses. However, when you include parentheses after the object name, C++ interprets it as a function declaration.
In our code snippet, when you write Foo foo2();, it is mistakenly interpreted as a function declaration of a function called foo2 that takes no arguments and returns a Foo object. This misunderstanding leads to the error when trying to access the member function bar of foo2.
The Solution π‘
Now that we understand the root cause of the error, fixing it is a breeze! To create an object using the constructor with no arguments, simply remove the parentheses from its declaration. Here's the corrected code:
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// This works...
Foo foo1(1);
foo1.bar();
// This now works too!
Foo foo2; // Parentheses removed
foo2.bar();
return 0;
}By removing the parentheses, the compiler correctly recognizes foo2 as an object declaration rather than a function declaration. Now you can happily access the member function bar without any errors!
Conclusion π
Congratulations! π You've successfully learned how to resolve the "error: request for member '...' in '...' which is of non-class type" issue when working with object-oriented programming in C++. By understanding the syntax pitfalls and applying the easy fix we discussed, you can now create objects using constructors without any headaches. β¨
If you found this blog post helpful, feel free to share it with your fellow developers who might be facing a similar struggle. Remember, understanding the problem is half the battle! Keep on coding and embrace those errors as opportunities to sharpen your skills. Happy coding! π»π
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



