473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using structs in C++

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[]

Jul 23 '06 #1
14 2383

Bit Byte wrote:
I have the following struct:

typedef struct
{
string symbol;
string synonym;
Synonym(string _synonym, string _symbol) {
synonym = _synonym;
symbol = _symbol;
}
} Synonym ;
You're stuck in C ville. Your definition should look more like:

struct Synonym
{
...
};

Jul 23 '06 #2
Bit Byte wrote:
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 ..
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.
>

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)
Your struct is not a POD. I think, there is no array-initializer syntax for
non-POD types.
Best

Kai-Uwe Bux
Jul 23 '06 #3

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...
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.
Jul 23 '06 #4
In article <cG*****************@wagner.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...
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.
Not in that particular context.
Jul 23 '06 #5

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
In article <cG*****************@wagner.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
>"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...
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.

Not in that particular context.
Variable names with leading underscores are
reserved. I wasn't aware that there are
any conditions where they are allowed.
Jul 23 '06 #6
Duane Hebert wrote:
>
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
>In article <cG*****************@wagner.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
>>"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...

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.

Not in that particular context.

Variable names with leading underscores are reserved.
That is an over-simplification. Please refer to section [17.4.3.1.2] for the
details.
I wasn't aware that there are any conditions where they are allowed.
A name like _symbol is not unconditionally reserved. It is only reserved in
global namespace and ::std. Thus, as a parameter name, it is fine (although
it might shadow a name from global namespace).
Best

Kai-Uwe Bux
Jul 23 '06 #7
In article <5p*****************@weber.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
In article <cG*****************@wagner.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...

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.
Not in that particular context.

Variable names with leading underscores are
reserved. I wasn't aware that there are
any conditions where they are allowed.
They are only reserved in the global namespace.
Jul 23 '06 #8

"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
>Variable names with leading underscores are
reserved. I wasn't aware that there are
any conditions where they are allowed.

They are only reserved in the global namespace.
Ok.
Jul 23 '06 #9
>>>As is the leading underscore in the variable names.
Not in that particular context.
Variable names with leading underscores are
reserved. I wasn't aware that there are
any conditions where they are allowed.

They are only reserved in the global namespace.
Not quite. A leading underscore followed by a capital is reserved
implementation-wide, as a leading double underscore (or any identifier
with a double underscore anywhere).
Jul 23 '06 #10
In article <PT*******************@newssvr27.news.prodigy.net> ,
red floyd <no*****@here.dudewrote:
>>As is the leading underscore in the variable names.
Not in that particular context.
Variable names with leading underscores are
reserved. I wasn't aware that there are
any conditions where they are allowed.
They are only reserved in the global namespace.

Not quite. A leading underscore followed by a capital is reserved
implementation-wide, as a leading double underscore (or any identifier
with a double underscore anywhere).
The context though was a leading underscore followed by a lower-case
letter.
Jul 24 '06 #11

Daniel T. wrote:
In article <cG*****************@wagner.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...
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.

Not in that particular context.
It may be ok as far as the language is concerned but imho it is still
bad practice. It is too easy to forget the particulars of when you can
vs. cannot use the underscore as first character in a name. Easier
just to never use it.

Jul 24 '06 #12

Noah Roberts wrote:
It may be ok as far as the language is concerned but imho it is still
bad practice. It is too easy to forget the particulars of when you can
vs. cannot use the underscore as first character in a name. Easier
just to never use it.
I seem to recall Herb Sutter in some book that was a collection of
"guru of the week" articles, suggesting that a trailing underscore
should be used in cases where the class member name was effectively the
same as the parameter name... Thus avoiding any implementation issues
and still making the mapping obvious.
Jon

Jul 24 '06 #13

Jon Clements wrote:
Noah Roberts wrote:
It may be ok as far as the language is concerned but imho it is still
bad practice. It is too easy to forget the particulars of when you can
vs. cannot use the underscore as first character in a name. Easier
just to never use it.

I seem to recall Herb Sutter in some book that was a collection of
"guru of the week" articles, suggesting that a trailing underscore
should be used in cases where the class member name was effectively the
same as the parameter name... Thus avoiding any implementation issues
and still making the mapping obvious.
Yeah, I'm yet to be convinced that is necissary.

Jul 24 '06 #14

"Daniel T." <da******@earthlink.netskrev i meddelandet
news:da****************************@news.west.eart hlink.net...
In article <5p*****************@weber.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:
>"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.ear thlink.net...
In article <cG*****************@wagner.videotron.net>,
"Duane Hebert" <sp**@flarn2.comwrote:

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:ea**********@murdoch.acc.Virginia.EDU...

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.

Not in that particular context.

Variable names with leading underscores are
reserved. I wasn't aware that there are
any conditions where they are allowed.

They are only reserved in the global namespace.
But as these names are reserved for gobal names, it is confusing to
also use them for local names. That makes it a bad practice.
Bo Persson

Jul 24 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sourin | last post by:
Hi all, I am trying to write code for my experiments. My work involves huge datasets, and as such my code needs to be memory efficient. I did some hand calculations regarding the amount of...
8
by: Mas L via DotNetMonster.com | last post by:
Hi, I have a c++ source code which I can compile to be a DLL (in VS.NET 2003). And I need to use it in a C# program. After I compiled/build the C++ code to a DLL, I add it as a Reference in my...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
6
by: DaTurk | last post by:
Hi, I'm coding a layer into my application using CLI, between a unmanaged and a c# layer. So, I have to marshal some unmanaged c++ structures to structures usable in c#. My solution was to...
6
by: titan nyquist | last post by:
Can I step through a struct using foreach? I want a method that steps through a struct, and prints out the name/value of each member. Can it be done? The error I get when trying this is: ...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
1
by: radskate360 | last post by:
Hi I am newer to programming and need a bit of help with this program. OK, heres the directions. The distance between two places on earth can be calculated by using their latitudes and...
2
by: =?Utf-8?B?U2V0aEluTUk=?= | last post by:
I am a total newb at .net, and I have not been able to search out a best practice answer to what must be a common problem. My app must process binary data from a UDP socket, a MSMQ queue and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.