静态方法 [英] static method

查看:52
本文介绍了静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为我的项目编写代码时,编译器给了我这个错误信息
''enterDoctor'' : ''static'' should not be used on member functions defined at file scope
其中(enterDoctor)是公共成员函数,我需要它是静态的.
我尝试上载源代码"it's not big:thumbsup:",但我不知道如何,所以我将发布最重要的说明.
在头文件"Doctor.h"中

as I was writing a code for my project ,the compiler gave me this error message
''enterDoctor'' : ''static'' should not be used on member functions defined at file scope
where (enterDoctor) is a public member function which I need it to be static.
I tried to upload the source code "it''s not big:thumbsup:" but I didn''t know how ,so I''ll post the most important instruction .
in the header file ''Doctor.h''

public:<br />
static void enterDoctor(char*&,char*&,char*&,float&);


实现``Doctor.cpp''


the implementation ''Doctor.cpp''

static void Doctor::enterDoctor(char*&fName,char*&lName,char*&ad,float &fee){
	cout<<"enter first name : ";cin>>fName;
	cout<<"enter last name : ";cin>>lName;
	cout<<"enter address : ";cin>>ad;
	cout<<"enter fee : ";cin>>fee;
	}


所以.....怎么想????问题在哪里!!!!!


so.....what do think ????? where is the problem !!!!!

推荐答案

如果您在这个位置,您可能还会有一个包含类声明的"doctor.h"看起来像这样:
If you are in this position, you''ll probably also have a "doctor.h" containing the class declaration looking like this:
class Doctor
{
    ...
public:
    static void enterDoctor(char*&fName,char*&lName,char*&ad,float &fee);
    ...
};



现在,在您的CPP文件中,定义此函数的主体,您不再需要说它是静态的(在文件级别静态意味着与在类级别不同).
只需将其声明为



Now, in your CPP file, to define the body of this function, you don''t need anymore to say it is static (static at file level means a different thing than at class level).
Just declare it as

void Doctor::enterDoctor(char*&fName,char*&lName,char*&ad,float &fee)
{
 ...
}



请注意,虚拟成员函数也存在此类问题.



Note that virtual member functions also have this kind of problem.


这篇关于静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆