cannot call member function without object

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:
        vectornames;
        vectorages;
        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.testor variable.read_names. How should I go about fixing this?

c++

shareimprove this question

edited Sep 18 '15 at 19:28

Ziezi

4,71632338

asked Jul 14 '09 at 20:15

trikker

1,19393047

  • I wonder why you want the two vector data attributes (names, ages)? Is one instance going to be used for more than one pair? – 2785528 Jul 17 at 14:00

  • Another thing to consider ... why the separate std::vector of names and ages instead of a single std::vector< Name_pairs >? – 2785528 Jul 17 at 14:03

add a comment

3 Answers

activeoldestvotes

up vote34down voteaccepted

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();
}

shareimprove this answer

edited Aug 24 '11 at 15:09

Jason Plank

2,12442638

answered Jul 14 '09 at 20:19

sth

162k40240331

add a comment

up vote24down vote

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

shareimprove this answer

answered Jul 14 '09 at 20:20

Rob K

7,74912329

  • Not possible in this case since all the methods need the object context to access one or more of the member properties names, ages, name, age. (they could be static, too. But then it would be quite useless ;-)) – VolkerK Jul 14 '09 at 20:28

  • thank you. correct for my case. forgot to put the 'static' keyword – jondinham Aug 23 '12 at 3:53

add a comment

up vote2down vote

You are right - you declared a new use defined type (Name_pairs) and you need variable of that type to use it.

The code should go like this:

Name_pairs np;
np.read_names()

shareimprove this answer

edited May 7 at 14:31

O'Neil

3,30321125

answered Jul 14 '09 at 20:21

dimba

11.1k25113174

add a comment

你可能感兴趣的:(embedded)