在 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 );
但我收到错误:
error: cannot declare member function 'static void IC_Utility::CP_StringToPString(std::string&, unsigned char*)' to have static linkage
我不明白为什么我不能这样做。谁能帮助我理解为什么以及如何实现我想要的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
删除方法定义中的
static
关键字。将其保留在您的类定义中。.cpp 文件中放置
static
关键字意味着某个函数具有静态链接,即。它只能从同一文件中的其他函数访问。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.定义中不应重复关键字
static
和virtual
。它们只能在类声明中使用。Keywords
static
andvirtual
should not be repeated in the definition. They should only be used in the class declaration.您不需要在函数定义中使用
static
You don't need to have
static
in function definition也许最好的做法是“像 std lib 那样做”。即:全部内联,全部在标题中。
就这么简单...
Probably the best course of action is "do it as std lib does it". That is: All inline, all in headers.
As simple as that ...
静态成员函数必须引用该类的静态变量。因此,在您的情况下,
由于您的成员函数 CP_StringToPstring 是静态的,因此该函数中的参数 inString 和 outString 也应该声明为静态。
静态成员函数不引用它正在处理的对象,但声明的变量引用其当前对象,因此它返回错误。
您可以从成员函数中删除静态,也可以添加静态,同时将用于成员函数的参数也声明为静态。
Static member functions must refer to static variables of that class. So in your case,
Since your member function
CP_StringToPstring
is static, the parameters in that function,inString
andoutString
should be declared as static too.The static member functions does not refer to the object that it is working on but the variables your declared refers to its current object so it return error.
You could either remove the static from the member function or add static while declaring the parameters you used for the member function as static too.