Define static method in source-file with declaration in header-file in C++
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?
Solution 1:
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.
Solution 2:
Keywords static
and virtual
should not be repeated in the definition. They should only be used in the class declaration.
Solution 3:
You don't need to have static
in function definition