为什么我不能在 VS2008 的类中使用静态成员,例如静态结构?
当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试创建静态成员 (pts) 的非静态成员 (ptCreate)。 这样是行不通的。
您有两个选择,要么使用 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.
Or, much safer (and better in my opinion), provide a constructor in Patterns and call that one.
On a side not, your code wouldn't work in any C++ compiler, it's not a conflict with Visual Studio things.
您只能将结构作为一个整体进行初始化,如下所示:
You can only initialize the structure as a whole, as in:
这不是有效的 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.
我不太确定你想在这里做什么。 看起来有点像您试图分别声明和初始化 pts 中的每个字段,而不是将 pts 声明为单个对象一次。 我真的很惊讶 VS 让你这么做。
在 gcc 中对我有用的是以下内容:
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: