微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

静态成员变量,不允许使用限定名称

如何解决静态成员变量,不允许使用限定名称

我在 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()
{
//....
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。