在C ++中使用结构 [英] Using structs in C++
问题描述
我有以下结构:
typedef struct
{
string symbol;
string同义词;
同义词(string _synonym,string _symbol){
synonym = _synonym;
symbol = _symbol;
}
}同义词;
编译时,我收到以下警告:
:警告C4183:''同义词'':缺少返回类型;假设是一个成员
函数返回''int''
我可以不为结构使用构造函数吗?我以前见过很多次
..
这是不好的做法吗?
我应该担心这个吗警告?
此外,我想用值填充此表。我想
将它声明为类中的静态成员 - 类似于:
同义词sym [] = {
....
};
无论如何我都找不到这样做(即没有编译错误)
static同义词syms []
I have the following struct:
typedef struct
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol) {
synonym = _synonym;
symbol = _symbol;
}
} Synonym ;
When I compile, I get the following warning:
: warning C4183: ''Synonym'': missing return type; assumed to be a member
function returning ''int''
Can I not use constructors for structs?. I''ve seen this done many times
before ..
Is this bad practise ?
Should I worry about this warning ?
Further more, I want to populate this table with values. I want to
declare it as a static member in a class - something like:
Synonyms sym[] = {
....
};
I cant find anyway to do this (i.e. without compilation errors)
static Synonym syms[]
推荐答案
Bit Byte写道:
Bit Byte wrote:
我有以下结构:
typedef struct
{
string symbol;
string同义词;
同义词(string _synonym,string _symbol){
synonym = _synonym;
symbol = _symbol;
}
}同义词;
I have the following struct:
typedef struct
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol) {
synonym = _synonym;
symbol = _symbol;
}
} Synonym ;
你被困在C ville。你的定义应该更像是:
struct同义词
{
...
} ;
You''re stuck in C ville. Your definition should look more like:
struct Synonym
{
...
};
Bit Byte写道:
Bit Byte wrote:
我有以下结构:
typedef struct
{
string symbol;
string synonym;
同义词(string _synonym,string _symbol){
synonym = _synonym;
symbol = _symbol;
}
}同义词;
当我编译时,我收到以下警告:
:警告C4183:''同义词'':缺少返回类型;假设是一个成员
函数返回''int''
我可以不为结构使用构造函数吗?我已经看过很多次了
之前..
I have the following struct:
typedef struct
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol) {
synonym = _synonym;
symbol = _symbol;
}
} Synonym ;
When I compile, I get the following warning:
: warning C4183: ''Synonym'': missing return type; assumed to be a member
function returning ''int''
Can I not use constructors for structs?. I''ve seen this done many times
before ..
你可以,但是编译器怎么知道呢
同义词(string _synonym,string _symbol)
是构造函数:它与结构的名称不匹配,
这是匿名的。你的typedef只声明同义词是
未命名结构的别名。如果你想使用像类一样的结构,请使用一个
有一个名字:
struct同义词
{
string symbol;
string synonym;
同义词(string _synonym,string _symbol){
synonym = _synonym;
symbol = _symbol;
}
};
BTW:您可能希望更喜欢初始化:
struct同义词
{
字符串符号;
字符串同义词;
同义词(string _synonym,string _symbol)
:symbol(_symbol)
,同义词(_synonym)
{}
};
You can, but how shall the compiler know that
Synonym(string _synonym, string _symbol)
is meant to be the constructor: it does not match the name of the struct,
which is anonymous. Your typedef just declares Synonym to be an alias for
the unnamed struct. If you want to use a struct like a class, use one that
has a name:
struct Synonym
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol) {
synonym = _synonym;
symbol = _symbol;
}
};
BTW: you may want to prefer initialization:
struct Synonym
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol)
: symbol ( _symbol )
, synonym ( _synonym )
{}
};
>
这是不好的做法?
>
Is this bad practise ?
编号遗留类型的设置是。
No. The legacy-typedef thing is.
我应该担心这个警告吗?
Should I worry about this warning ?
是。
Yes.
>
更多,我想用值填充此表。我想
将它声明为类中的静态成员 - 类似于:
同义词sym [] = {
....
};
无论如何我都找不到(即没有编译错误)
>
Further more, I want to populate this table with values. I want to
declare it as a static member in a class - something like:
Synonyms sym[] = {
....
};
I cant find anyway to do this (i.e. without compilation errors)
您的结构不是POD。我认为,
非POD类型没有数组初始化语法。
最佳
Kai-Uwe Bux
Your struct is not a POD. I think, there is no array-initializer syntax for
non-POD types.
Best
Kai-Uwe Bux
" Kai-Uwe Bux" < jk ******** @ gmx.netwrote in message
news:ea ********** @ murdoch.acc.Virginia.EDU ...
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...
BTW:您可能希望更喜欢初始化:
struct同义词
{
字符串符号;
字符串同义词;
同义词(字符串_synonym,字符串_symbol)
:符号(_symbol)
,同义词(_同义词)
{}
};
BTW: you may want to prefer initialization:
struct Synonym
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol)
: symbol ( _symbol )
, synonym ( _synonym )
{}
};
>>
这是不好的做法吗?
>>
Is this bad practise ?
编号遗留类型的东西是。
No. The legacy-typedef thing is.
与变量名中的前导下划线一样。
As is the leading underscore in the variable names.
这篇关于在C ++中使用结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!