When I write code like this in VS 2008:
当我在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'
'Patterns':'::'左边的符号必须是'ptSelect'类型:不是'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.
我究竟做错了什么?这是Visual Studio或我的代码的问题吗?我知道除了const之外的静态成员必须在声明它们的类之外定义。
4 个解决方案
#1
10
You're trying to create a non-static member (ptCreate) of a static member (pts). This won't work like this.
您正在尝试创建静态成员(pts)的非静态成员(ptCreate)。这不会像这样工作。
You got two options, either use a struct initializer list for the Patterns class.
您有两个选项,要么为Patterns类使用struct初始化列表。
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.
或者,更安全(在我看来更好),在Patterns中提供构造函数并调用它。
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.
不是这样,你的代码在任何C ++编译器中都不起作用,它与Visual Studio的东西没有冲突。
#2
3
You can only initialize the structure as a whole, as in:
您只能将结构初始化为一个整体,如:
Patterns QueryValidate::pts = { "something", "something", ... };
#3
1
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:
这不是有效的C ++。在cpp文件中,您声明了静态结构“QueryValidate :: pts”的一部分,但这是不允许的:您必须声明整个结构,如下所示:
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.
如果您想要初始化成员,您可以在另一个方法中初始化它们,或者向Pattern中添加构造函数,以获取您想要的任何初始化参数。
#4
0
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.
我不确定你在这里要做什么。它看起来有点像你试图分别声明和初始化pts中的每个字段,而不是将pts声明为单个对象。我真的很惊讶VS让你这样做。
What worked for me in gcc was the following:
在gcc中对我有用的是:
Patterns QueryValidate::pts;
void foo () {
QueryValidate::pts.ptCreate = "something";
QueryValidate::pts.ptDelete = "something";
}
#1
10
You're trying to create a non-static member (ptCreate) of a static member (pts). This won't work like this.
您正在尝试创建静态成员(pts)的非静态成员(ptCreate)。这不会像这样工作。
You got two options, either use a struct initializer list for the Patterns class.
您有两个选项,要么为Patterns类使用struct初始化列表。
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.
或者,更安全(在我看来更好),在Patterns中提供构造函数并调用它。
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.
不是这样,你的代码在任何C ++编译器中都不起作用,它与Visual Studio的东西没有冲突。
#2
3
You can only initialize the structure as a whole, as in:
您只能将结构初始化为一个整体,如:
Patterns QueryValidate::pts = { "something", "something", ... };
#3
1
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:
这不是有效的C ++。在cpp文件中,您声明了静态结构“QueryValidate :: pts”的一部分,但这是不允许的:您必须声明整个结构,如下所示:
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.
如果您想要初始化成员,您可以在另一个方法中初始化它们,或者向Pattern中添加构造函数,以获取您想要的任何初始化参数。
#4
0
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.
我不确定你在这里要做什么。它看起来有点像你试图分别声明和初始化pts中的每个字段,而不是将pts声明为单个对象。我真的很惊讶VS让你这样做。
What worked for me in gcc was the following:
在gcc中对我有用的是:
Patterns QueryValidate::pts;
void foo () {
QueryValidate::pts.ptCreate = "something";
QueryValidate::pts.ptDelete = "something";
}