Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot call member function without object

Tags:

c++

This program has the user input name/age pairs and then outputs them, using a class. Here is the code.

#include "std_lib_facilities.h"  class Name_pairs { public:        bool test();        void read_names();        void read_ages();        void print(); private:         vector<string>names;         vector<double>ages;         string name;         double age; };  void Name_pairs::read_names() {      cout << "Enter name: ";      cin >> name;      names.push_back(name);      cout << endl; }  void Name_pairs::read_ages() {      cout << "Enter corresponding age: ";      cin >> age;      ages.push_back(age);      cout << endl; }  void Name_pairs::print() {      for(int i = 0; i < names.size() && i < ages.size(); ++i)              cout << names[i] << " , " << ages[i] << endl; }  bool Name_pairs::test() {    int i = 0;    if(ages[i] == 0 || names[i] == "0") return false;    else{         ++i;         return true;} }   int main() {     cout << "Enter names and ages. Use 0 to cancel.\n";     while(Name_pairs::test())     {      Name_pairs::read_names();      Name_pairs::read_ages();      }      Name_pairs::print();      keep_window_open(); } 

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?

like image 334
trikker Avatar asked Jul 14 '09 20:07

trikker


People also ask

How do you call a function without an object?

It's time to call the function to execute it without using anything else. So, we have simply used the class name “A” with “::” to call the function “show()” in the main method. The main method is closed here. We are ready to save this code with Ctrl+S as it is already complete.

How do you call a function without an object in C++?

You could declare the function outside the vector class but in the same namespace/file and then define it accordingly. And then in the cpp: namespace math { Vector::Vector() { ... } double scalar(const Vector& v1, const Vector& v2) { ... } }

Can we call member function in constructor C++?

The problem with calling virtual member functions from a constructor is that a subclass can override the function. This will cause the constructor to call the overridden implementation in the subclass, before the constructor for the subclass part of the object has been called.

Can we call a non member function inside the class?

The answer is YES.


2 Answers

You need to instantiate an object in order to call its member functions. The member functions need an object to operate on; they can't just be used on their own. The main() function could, for example, look like this:

int main() {    Name_pairs np;    cout << "Enter names and ages. Use 0 to cancel.\n";    while(np.test())    {       np.read_names();       np.read_ages();    }    np.print();    keep_window_open(); } 
like image 151
sth Avatar answered Sep 20 '22 15:09

sth


If you want to call them like that, you should declare them static.

like image 42
Rob K Avatar answered Sep 18 '22 15:09

Rob K