Define static method in source-file with declaration in header-file in C++

C++Static Linking

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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 - C++

Keywords static and virtual should not be repeated in the definition. They should only be used in the class declaration.

Solution 3 - C++

You don't need to have static in function definition

Solution 4 - C++

Probably the best course of action is "do it as std lib does it". That is: All inline, all in headers.

// in the header
namespase my_namespace {

   class my_standard_named_class final {
public:
         static void standard_declared_defined_method () {
            // even the comment is standard
         }
   } ;

} // namespase my_namespace 

As simple as that ...

Solution 5 - C++

Static member functions must refer to static variables of that class. So in your case,

static void CP_StringToPString( std::string& inString, unsigned char *outString);

Since your member function CP_StringToPstring is static, the parameters in that function, inString and outString 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionABVView Question on Stackoverflow
Solution 1 - C++x13nView Answer on Stackoverflow
Solution 2 - C++Bo PerssonView Answer on Stackoverflow
Solution 3 - C++cpxView Answer on Stackoverflow
Solution 4 - C++Chef GladiatorView Answer on Stackoverflow
Solution 5 - C++Prince AloiesView Answer on Stackoverflow