Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static function: a storage class may not be specified here

Tags:

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!

like image 436
sccs Avatar asked Mar 31 '13 03:03

sccs


People also ask

Can a function be declared with a static storage class?

A function declared with the static storage class specifier has internal linkage, which means that it may be called only within the translation unit in which it is defined. The default for a function is external linkage. The only storage class that can be specified for a function parameter is register.

How do you make a class static in C++?

There is no such thing as a static class in C++. The closest approximation is a class that only contains static data members and static methods. Static data members in a class are shared by all the class objects as there is only one copy of them in the memory, regardless of the number of objects of the class.

What is static storage duration in C++?

static storage duration. The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with static or extern .


1 Answers

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

like image 136
Adam Rosenfield Avatar answered Nov 01 '22 14:11

Adam Rosenfield