Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

member function
object
error
without
c++

Resolving the "cannot call member function without object" error

Educative Answers Team

The “cannot call member function without object” error is a common C++ error that occurs while working with classes and objects. The error is thrown when one tries to access the functions of a class without instantiating it.

svg viewer

Code

Consider the following C++ code snippets that depict the code with the error scenario and the correct code. The solution is to create an object of a class (instantiation) before using its methods.

#include <iostream>
using namespace std;

class Employee
{
  public:
  string name = "John";
  int age = 25;

  void printData()
  {
    cout << "Employee Name: " << name << endl;
    cout << "Employee Age: " << age << endl;
  }
};

int main() 
{
  Employee::printData();
  return 0;
}

RELATED TAGS

member function
object
error
without
c++
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring