25

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!

4

1 回答 1

51

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

于 2013-03-31T03:10:46.190 回答