• Register
1 vote
139 views

Problem:

First of all, I apologize to all of you guys. It might be a noob question but I couldn't figure out the problem. I was trying to print some data from a class file in my main function. The program never returned my expected result but an error cannot call member function without object.

Here is my code snippet.

#include <iostream>
using namespace std;
class Student{
  public:
  string name = "Roy";
  int age = 27;

  void printValue(){
    cout << "Student Name: " << name << endl;
    cout << "Student Age: " << age << endl;
  }
};

int main(){
  Student::printValue();
  return 0;
}

Which point I am doing wrong? Could you please sort it out?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

The cannot call member function without objecterror is a common C++ error that occurs while working with classes and objects. The error is thrown when someone tries to access the functions of a class without instantiating it. Your question was, how to get rid of this? simple, just create an object of that class and call the object from your main function before using its methods. Lrt’s create an object of your public class and use its method inside the man function.

#include <iostream>
using namespace std;
class Student{
  public:
  string name = "Roy";
  int age = 27;

  void printValue(){
    cout << "Student Name: " << name << endl;
    cout << "Student Age: " << age << endl;
  }
};

int main(){
  Student myobj; // Creating an object
  myobj.printValue(); // Calling method
  return 0;
}

Now you are all set to go and the program should give back your expected output:

Student Name: Roy
Student Age: 27

Keep asking without any hesitation. 

13 9 6
94,260 points

Related questions

0 votes
1 answer 22 views
22 views
Problem: This program has the user input name/age pairs and then outputs them, using a class. Here is the code.However, in int main() when I'm trying to call the functions I get "cannot call 'whatever name is' function without object." I'm guessing this is because it's looking for something like variable.test or variable.read_names. How should I go about fixing this?
asked Dec 13, 2020 Mashhoodch 13k points
0 votes
1 answer 6 views
6 views
Problem: I keep encountering this error when I try to compile > Error: call to non-static member function without an object argument
asked May 3 ochena 23.3k points
0 votes
1 answer 443 views
443 views
Problem: I want to use the three threads to execute a non static member function of the objects contained within another object. My code does not compile. "Call to non-static member function without an object argument." I face the above error if I try to pass the pointer to each ... a.add_b(2); a.add_b(3); a.do_someting(); return 0; } Looking for guidance from C++ expert to fix my above code.
asked Aug 11, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 6 views
6 views
Problem: I need to solve this matter, please help >Call to non static member function without an object argument.
asked 6 days ago Danyal Sjoerd 6.8k points
0 votes
1 answer 336 views
336 views
Problem : Error: No matching member function for call to 'push_back'
asked Feb 23 Sabbir Hossain 14.6k points
0 votes
1 answer 3 views
3 views
Problem: Where ought to I seek to simplify this issue? > Call to a member function fetch_assoc() on a non-object .
asked 2 days ago Isac Christiaan 9.6k points
0 votes
1 answer 3 views
3 views
Problem: Please help me &ldquo;Call to a member function bind_param() on a non-object&rdquo;?
asked Apr 6 tuhin1 48.9k points
0 votes
1 answer 8 views
8 views
Problem: When I encountered this problem, I attempted to find a solution on the internet but was unable to find any useful details. If you read the above query and understand what the solution is, please cooperate with me- Thanks.
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 11 views
11 views
Problem: When I encountered this problem, I attempted to find a solution on the internet but was unable to find any useful details. If you read the above query and understand what the solution is, please cooperate with me- Thanks.
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 13 views
13 views
Problem: I am unable to solve it, please solve this problem ... Call to a member function bind_param() on a non-object in
asked Apr 1 Ifra 37.2k points