C ++中的静态方法 [英] Static methods in C++

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

问题描述

我在使用C ++中的静态方法时遇到一些麻烦



示例.h:

  class IC_Utility {
public:
IC_Utility();
〜IC_Utility();

std :: string CP_PStringToString(const unsigned char * outString);
void CP_StringToPString(std :: string& inString,unsigned char * outString,short inMaxLength);
static void CP_StringToPString(std :: string& inString,unsigned char * outString);
void CP_StringToPString(FxString& inString,FxUChar * outString);

};

示例.cpp:

  static void IC_Utility :: CP_StringToPString(std :: string& inString,unsigned char * outString)
{
short length = inString.length

if(outString!= NULL)
{
if(length> = 1)
CPLAT :: CP_Utility :: CP_CopyMemory(inString.c_str & outString [1],length);

outString [0] = length;
}
}

我想打一个电话:

  IC_Utility :: CP_StringToPString(directoryNameString,directoryName); 

但我得到一个错误:

 错误:无法声明成员函数'static void IC_Utility :: CP_StringToPString(std :: string& unsigned char *)'以具有静态链接
/ pre>

我不明白为什么我不能这样做。

解决方案

删除 static 方法定义中的关键字。



static 关键字放在.cpp文件中意味着某个函数有一个静态连接,即。它只能从同一文件中的其他函数访问。


I am having a little trouble working with static methods in C++

Example .h:

class IC_Utility {
public:
    IC_Utility();
    ~IC_Utility();

    std::string CP_PStringToString( const unsigned char *outString );
    void CP_StringToPString( std::string& inString, unsigned char *outString, short inMaxLength );
    static void CP_StringToPString( std::string& inString, unsigned char *outString);
    void CP_StringToPString( FxString& inString, FxUChar *outString);

};

Example .cpp:

static void IC_Utility::CP_StringToPString(std::string& inString, unsigned char *outString)
{
    short       length = inString.length();

   if( outString != NULL )
    {
        if( length >= 1 )
            CPLAT::CP_Utility::CP_CopyMemory( inString.c_str(), &outString[ 1 ], length );

            outString[ 0 ] = length;
    }
}

I wanted to make a call like:

IC_Utility::CP_StringToPString(directoryNameString, directoryName );

But I get an error:

error: cannot declare member function 'static void IC_Utility::CP_StringToPString(std::string&, unsigned char*)' to have static linkage

I dont understand why I cannot do this. Can anyone help me understand why and how to achieve what I want?

解决方案

Remove static keyword in method definition. Keep it just in your class definition.

static keyword placed in .cpp file means that a certain function has a static linkage, ie. it is accessible only from other functions in the same file.

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

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