在C ++中的头文件中声明的源文件中定义静态方法 [英] Define static method in source-file with declaration in header-file in C++

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

问题描述

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

I am having a little trouble working with static methods in 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 );

但是我得到一个错误:

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 关键字放置在.cpp文件中意味着某个函数具有一个静态链接,即它只能从同一文件中的其他函数访问。

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天全站免登陆