C++ 没有对象就不能调用成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1127738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:55:21  来源:igfitidea点击:

cannot call member function without object

c++

提问by trikker

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

该程序具有用户输入name/age配对,然后使用类输出它们。这是代码。

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

但是,int main()当我尝试调用我得到的函数时,我"cannot call 'whatever name is' function without object."猜这是因为它正在寻找类似variable.testor 的东西variable.read_names。我应该如何解决这个问题?

回答by sth

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:

您需要实例化一个对象才能调用其成员函数。成员函数需要一个对象来操作;它们不能单独使用。main()例如,该函数可能如下所示:

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

回答by Rob K

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

如果你想这样调用它们,你应该声明它们是静态的。

回答by dimba

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

您是对的 - 您声明了一个新的使用定义类型(Name_pairs),并且您需要该类型的变量才能使用它。

The code should go like this:

代码应该是这样的:

Name_pairs np;
np.read_names()

回答by Ketan Vishwakarma

just add statickeyword at the starting of the function return type.. and then you can access the member function of the class without object:) for ex:

只需在函数返回类型的开头添加static关键字..然后您就可以访问没有对象的类的成员函数:)例如:

static void Name_pairs::read_names()
{
   cout << "Enter name: ";
   cin >> name;
   names.push_back(name);
   cout << endl;
}