scc*_*ccs 18 c++ static class visual-studio-2010
我已经以这种方式在我的类中定义了一个函数static(相关代码片段)
#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
Run Code Online (Sandbox Code Playgroud)
并在.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
}
Run Code Online (Sandbox Code Playgroud)
我在.cpp文件中收到错误(VS C++ 2010),说"这里可能没有指定存储类",我无法弄清楚我做错了什么.
ps我已经读过这看起来像重复但我不知道 - 就像他一样 - 我是对的,编译器很挑剔.任何帮助表示赞赏,我找不到任何关于此的信息!
Ada*_*eld 35
在.cpp文件的定义中,删除关键字static:
// No static here (it is not allowed)
std::string neighborAtt::intToStr(int number)
{
...
}
Run Code Online (Sandbox Code Playgroud)
只要static在头文件中有关键字,编译器就知道它是一个静态类方法,所以你不应该也不能在源文件的定义中指定它.
在C++ 03中,存储类声明是关键字auto,register,static,extern,和mutable,它告诉编译器的数据存储方式.如果您看到引用存储类说明符的错误消息,则可以确定它指的是其中一个关键字.
在C++ 11中,auto关键字具有不同的含义(它不再是存储类说明符).
| 归档时间: |
|
| 查看次数: |
22062 次 |
| 最近记录: |