164

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?

ABV
  • 1,645
  • 2
  • 11
  • 5
  • 3
    First of all, you should remove the `static` keyword in the .cpp file. C++ does not permit it. – B. Decoster May 12 '11 at 15:36
  • 12
    @Fezvez: Alternately, replace it with `/* static */`. I like having the same modifiers and default arguments in the .h and .cpp files. – David Thornley May 12 '11 at 15:40
  • 3
    TL;DR: Keep `static` in the header file `.h`, it means "attached to class, not to any object", remove `static` in the `.cpp` file, it has a different meaning which you do not want here. – Stéphane Gourichon Aug 04 '18 at 21:56

5 Answers5

264

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.

x13n
  • 4,103
  • 2
  • 21
  • 28
  • 1
    Ah, Got it so `static` in the method definition would mean only other methods in that class can access that static method, no other methods outside that class. – ABV May 12 '11 at 15:42
  • 14
    Not other class methods, but other functions in .cpp file. You should not do this in C++ anyway. If you want a C++ function to have internal linkage, you should consider placing it in some anonymous namespace. Usage of `static` in .cpp files is just for backward compatibility with C. – x13n May 12 '11 at 15:46
  • 1
    Just for curiosity... If I define a static class member directly in the class (in the .h file), how could I use static linkage? – lumbric Oct 08 '13 at 09:35
  • You can't. And it makes no sense to do so, since linking the program together would cause unresolved externals to appear. – x13n Oct 08 '13 at 10:41
48

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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
14

You don't need to have static in function definition

cpx
  • 17,009
  • 20
  • 87
  • 142
0

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 ...

Chef Gladiator
  • 902
  • 11
  • 23
-4

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.

Morix Dev
  • 2,700
  • 1
  • 28
  • 49
  • 2
    inString and outString are arguments of static function. They are not class members. There is no need of converting them to static. – 999k Dec 09 '14 at 11:08
  • That is not correct at all. You can place non static arguments in a static member function. But of class members, you can only access / modify the static ones in the function. – Zachary Kraus Mar 23 '15 at 04:12