Why can't I use static elements like static structures in my classes in VS2008?

When I write this code in VS 2008:

.h
struct Patterns {
        string ptCreate;
        string ptDelete;
        string ptDrop;
        string ptUpdate;
        string ptInsert;
        string ptSelect;
    };     

class QueryValidate {
    string query;
    string pattern;
    static Patterns pts;
public:
    friend class Query;
    QueryValidate(const string& qr, const string& ptn):
      query(qr), pattern(ptn) {}
    bool validate() {
        boost::regex rg(pattern);
        return boost::regex_match(query, rg);
    }
    virtual ~QueryValidate() {}
};

Then I initialize my structure as follows:

.cpp
string QueryValidate::pts::ptCreate = "something";
string QueryValidate::pts::ptDelete = "something";
//...

The compiler gives the following errors:

'Templates': the character to the left of '::' must be of type 'ptSelect': not a member of QueryValidate

What am I doing wrong? Is this a problem with Visual Studio or with my code? I know that static members, with the exception of const, must be defined outside the class in which they were declared.

+3
source share
4 answers

You are trying to create a non-static member (ptCreate) of a static member (pts). This will not work like that.

: Patterns.

Patterns QueryValidate::pts = {"CREATE", "DELETE"}; // etc. for every string

, ( , ), Patterns .

struct Patterns {
   Patterns() { /*...*/ }
   /* ... */
}

, ++, Visual Studio.

+10

, :

Patterns QueryValidate::pts = { "something", "something", ... };
+3

++. cpp "QueryValidate:: pts", : , :

QueryValidate:: pts;

, , , , , .

+1

I'm not sure what you are trying to do here. It looks like you are trying to declare and initialize each field in pts separately, rather than declaring pts once as a single object. I am really surprised that VS allows you to do this.

What worked for me in gcc was the following:

Patterns QueryValidate::pts;

void foo () {
    QueryValidate::pts.ptCreate = "something";
    QueryValidate::pts.ptDelete = "something";
}
0
source

Source: https://habr.com/ru/post/1699278/


All Articles