SimpleMind
SimpleMind

Reputation: 29

Static Member Variable, Qualified Name is Not Allowed

I've searched other answer's on SO and feel like I'm following the format correctly, so not sure what Im doing wrong and am at my wits end with this one....

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"};
}

In VS, I get red squiggles in main.cpp under DBwrapper::DBfile saying "Qualified name is not allowed". I've tried with char [] as well as const/not const.

Upvotes: 0

Views: 650

Answers (2)

SimpleMind
SimpleMind

Reputation: 29

Yeah, so, basically don't have it IN the int main(){} function. /facepalms

#include "DBwrapper.h"
#include <string>

std::string DBwrapper::DBfile = "file";
int main()
{
//....
}

Upvotes: 0

lubgr
lubgr

Reputation: 38287

You need to define the static data member at namespace scope, not in a function like 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
}

Upvotes: 4

Related Questions