2013-03-31 22 views
8

指定我已经以这种方式定义的函数,如静态在我的课(相关代码段)静态功能:存储类可能不被这里

#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 

,并在.cpp文件作为

#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 
} 

我得到一个错误(VS C++ 2010)在.cpp文件中说“存储类可能不在此处指定”,我无法弄清楚我做错了什么。

p.s.我已经阅读this这看起来像一个重复的,但我不知道 - 就像他一样 - 我是对的,编译器是挑剔的。任何帮助表示赞赏,我无法找到关于此的任何信息!

回答

19

.cpp文件中的定义,删除关键字static

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

只要你在头文件中的static关键字,编译器知道它是一个静态类方法,所以你不应该并且无法在源文件的定义中指定它。

在C++中03中,存储类说明是关键字autoregisterstaticextern,和mutable,这告诉编译器中的数据是如何存储的。如果您看到有关存储类说明符的错误消息,则可以确定它指的是其中一个关键字。

在C++ 11中,auto关键字具有不同的含义(它不再是存储类说明符)。

+0

你确定'可变'吗?它在BNF中显示为一个* storage-class-specifier *,但它不像一个行为。 'thread_local'是C++ 11中的存储类说明符。 –

+0

@ BenVoigt:是的,C++ 03§7.7.1明确列出了这5个说明符。 –