Static function: a storage class may not be specified here

37,065

In the definition in the .cpp file, remove the keyword static:

// No static here (it is not allowed)
std::string neighborAtt::intToStr(int number)
{
    ...
}

As long as you have the static keyword in the header file, the compiler knows it's a static class method, so you should not and cannot specify it in the definition in the source file.

In C++03, the storage class specifiers are the keywords auto, register, static, extern, and mutable, which tell the compiler how the data is stored. If you see an error message referring to storage class specifiers, you can be sure it's referring to one of those keywords.

In C++11, the auto keyword has a different meaning (it is no longer a storage class specifier).

Share:
37,065

Related videos on Youtube

sccs
Author by

sccs

Updated on July 15, 2022

Comments

  • sccs
    sccs almost 2 years

    I have defined a function as static in my class in this manner (snippet of relevant code)

    #ifndef connectivityClass_H
    #define connectivityClass_H
    
    class neighborAtt
    {
    public:
        neighborAtt(); //default constructor
        neighborAtt(int, int, int);
    
        ~neighborAtt(); //destructor
    
        static std::string intToStr(int number);
    
    private:
        int neighborID;
        int attribute1;
        int attribute2;
    
    #endif
    

    and in the .cpp file as

    #include "stdafx.h"
    #include "connectivityClass.h"
    
    static std::string neighborAtt::intToStr(int number)
    {
        std::stringstream ss; //create a stringstream
       ss << number; //add number to the stream
       return ss.str(); //return a string with the contents of the stream
    }
    

    and I get an error (VS C++ 2010) in the .cpp file saying "A storage class may not be specified here" and I cannot figure out what I'm doing wrong.

    p.s. I've already read this which looks like a duplicate but I don't know - as he does - that I am right and the compiler is being finicky. Any help is appreciated, I can't find any information on this!

  • Ben Voigt
    Ben Voigt about 11 years
    Are you sure about mutable? It appears as a storage-class-specifier in the BNF, but it doesn't behave as one. And thread_local IS a storage class specifier in C++11.
  • Adam Rosenfield
    Adam Rosenfield about 11 years
    @BenVoigt: Yes, C++03 §7.7.1 explicitly lists out those 5 specifiers.