• Register
1 vote
53 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,240 points

Related questions

0 votes
1 answer 9 views
9 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 9.1k points
0 votes
1 answer 261 views
261 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 4 views
4 views
Problem : Error: No matching member function for call to 'push_back'
asked 3 days ago Shovo210 2.6k points
0 votes
1 answer 329 views
329 views
Problem : I have my class called MyPerson: class MyPerson {     string myname;     long myscore; public:     MyPerson(string myname="", long myscore=0);     void setMyName(string myname);     void setMyScore(long myscore);     string getMyName();     long ... not compatible with the member function with red lines under the the 2 people[j] in the print method Am I doing something wrong here?
asked Jan 4, 2020 alecxe 7.5k points
0 votes
1 answer 7 views
7 views
Problem: What is a const member function in C++? What is the syntax of a const member function? How do you declare a constant in C++? What does const after a function mean?How member function can be called on a const object (or via a reference-or-pointer-to-const), and that the member function cannot modify data members (unless they're marked mutable ?
asked Dec 23, 2020 Mashhoodch 9.1k points
0 votes
1 answer 9 views
9 views
Problem: What is private member function in C++? How do you use private members in C++? What is member function with example? What is data member in C++ with example? How can i declare a function/class as friend of that particular class, and then the member will be accessible inside that function or class object without access specifier check?
asked Dec 23, 2020 Mashhoodch 9.1k points
0 votes
1 answer 49 views
49 views
Problem: I am working with classes in C++ and trying to access a function that is private but continuously failing to do so. How can we access private member functions?
asked Oct 13, 2020 Code Learner 5.9k points
0 votes
1 answer 9 views
9 views
Problem: What is reverse string in c++ without using function
asked Feb 14 jasimchaudary 640 points
2 votes
1 answer 618 views
618 views
Problem : I have few years of experience in working with C programming. Now I am preparing for the interview and while preparing for it I came to below question for which I don&rsquo;t have any working code. &ldquo;C program to find square root of a number without using sqrt function&rdquo;. Can somebody help me in answering above question?
asked Mar 24, 2020 morrisBson 3.2k points
0 votes
1 answer 14 views
14 views
I have this code: int max1 (int a, int b) {int m = a;  if (b> m) {m = b;  } return m;  } int main () {cout<< max1 (5,6) <<endl;  cout<< main () <<endl;  return 0;  } I have wanted to test that code but am afraid my computer will be damaged. What happens if the main () function is called?
asked Oct 1, 2020 sasha 12.9k points