问题描述
我在 SO 上搜索了其他答案,感觉我正确地遵循了格式,所以不确定我做错了什么,并且我对这个问题感到不知所措....
class DBwrapper {
private:
static std::string DBfile;
//static char DBfile[];
#include "DBwrapper.h"
#include <string>
int main()
{
std::string DBwrapper::DBfile = "library.db3";
std::string DBwrapper::DBfile{"library.db3"};
}
在 VS 中,我在 DBwrapper::DBfile 下的 main.cpp 中看到红色波浪线,说“不允许使用限定名称”。 我试过 char [] 以及 const/not const。
解决方法
您需要在命名空间范围内定义 static
数据成员,而不是在 main
之类的函数中:
#include "DBwrapper.h"
#include <string>
// Static data members don't have anything to do with functions
// and their scopes,so this must appear here:
std::string DBwrapper::DBfile = "library.db3";
int main()
{
// Here,you would rather instantiate DBwrapper to work with it
}
,
是的,所以,基本上没有它在 int main(){} 函数中。 /面掌
#include "DBwrapper.h"
#include <string>
std::string DBwrapper::DBfile = "file";
int main()
{
//....
}