为什么我不能在 VS2008 的类中使用静态成员,例如静态结构?

发布于 2024-07-09 05:24:00 字数 972 浏览 4 评论 0原文

当我在 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() {}
};

然后我像这样初始化我的结构:

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

编译器给出以下错误:

“Patterns”:“::”左侧的符号必须是“ptSelect”类型 : 不是“QueryValidate”的成员

我做错了什么? 这是 Visual Studio 的问题还是我的代码的问题? 我知道除 const 之外的静态成员必须在声明它们的类之外定义。

When I write code like this 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() {}
};

I then initialize my structure like this:

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

The compiler gives the following errors:

'Patterns': the symbol to the left of a '::' must be a type 'ptSelect'
: is 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 except for const ones must be defined outside the class they were declared in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叹沉浮 2024-07-16 05:24:00

您正在尝试创建静态成员 (pts) 的非静态成员 (ptCreate)。 这样是行不通的。

您有两个选择,要么使用 Patterns 类的结构初始值设定项列表。

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

或者,更安全(在我看来更好),在模式中提供一个构造函数并调用它。

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

另一方面,您的代码无法在任何 C++ 编译器中运行,这与 Visual Studio 不存在冲突。

You're trying to create a non-static member (ptCreate) of a static member (pts). This won't work like this.

You got two options, either use a struct initializer list for the Patterns class.

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

Or, much safer (and better in my opinion), provide a constructor in Patterns and call that one.

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

On a side not, your code wouldn't work in any C++ compiler, it's not a conflict with Visual Studio things.

归属感 2024-07-16 05:24:00

您只能将结构作为一个整体进行初始化,如下所示:

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

You can only initialize the structure as a whole, as in:

Patterns QueryValidate::pts = { "something", "something", ... };
不乱于心 2024-07-16 05:24:00

这不是有效的 C++。 在 cpp 文件中,您声明静态结构“QueryValidate::pts”的一部分,但这是不允许的:您必须声明整个结构,如下所示:

Patterns QueryValidate::pts;

如果您希望初始化成员,您可以在另一个方法中初始化它们,或者向 Patterns 添加一个构造函数来接受您想要的任何初始化参数。

This isn't valid C++. In the cpp file you're declaring parts of the static structure "QueryValidate::pts", but that's not allowed: you've got to declare the whole structure, like so:

Patterns QueryValidate::pts;

if you want members to be initialized, you either initialize them in another method, or add a constructor to Patterns that takes whatever initialization arguments you want.

一个人的夜不怕黑 2024-07-16 05:24:00

我不太确定你想在这里做什么。 看起来有点像您试图分别声明和初始化 pts 中的每个字段,而不是将 pts 声明为单个对象一次。 我真的很惊讶 VS 让你这么做。

在 gcc 中对我有用的是以下内容:

Patterns QueryValidate::pts;

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

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

What worked for me in gcc was the following:

Patterns QueryValidate::pts;

void foo () {
    QueryValidate::pts.ptCreate = "something";
    QueryValidate::pts.ptDelete = "something";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文