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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my