473,770 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2430

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.vi deotron.net>,
"Duane Hebert" <sp**@flarn2.co mwrote:
"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******@earth link.netwrote in message
news:da******** *************** *****@news.west .earthlink.net. ..
In article <cG************ *****@wagner.vi deotron.net>,
"Duane Hebert" <sp**@flarn2.co mwrote:
>"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******@earth link.netwrote in message
news:da******** *************** *****@news.west .earthlink.net. ..
>In article <cG************ *****@wagner.vi deotron.net>,
"Duane Hebert" <sp**@flarn2.co mwrote:
>>"Kai-Uwe Bux" <jk********@gmx .netwrote in message
news:ea****** ****@murdoch.ac c.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.vid eotron.net>,
"Duane Hebert" <sp**@flarn2.co mwrote:
"Daniel T." <da******@earth link.netwrote in message
news:da******** *************** *****@news.west .earthlink.net. ..
In article <cG************ *****@wagner.vi deotron.net>,
"Duane Hebert" <sp**@flarn2.co mwrote:
"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******@earth link.netwrote in message
news:da******** *************** *****@news.west .earthlink.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

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

Similar topics

3
2712
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 dynamic memory required for my datastructures ( used sizeof() to get the size of the structures ). But mallinfo() function show that approximately double that amount of memory is being allocated. I am working with gcc 3.2.2 on a redhat 9 machine. I...
8
3615
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 C# program. When I look at the reference and I double click to view it in the objec browser , I see only structs , without their members. And I don't see methods.
5
2917
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 that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
61
3781
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 (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
6
8153
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 marshal the unmanaged C++ structures to CLI ref structs, these being usable in the c# layer. Why I did it this way is I want all of the business logic in the CLI layer. Because he c# layer is purely presentaion.
6
13213
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: "foreach statement cannot operate on variables of type '...' because '...' does not contain a public definition for 'GetEnumerator'. And it points me to http://msdn2.microsoft.com/en-us/library/t51esaeb(vs.80).aspx I guess this is the answer, but...
29
2792
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 overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
43
3835
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 "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
1
4771
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 longitudes. The calculation for this is as follows: (The latitudes and longitudes must be converted to radians (radians=degrees * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979323846 Earth's Radius = 3963.1
2
1916
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 file. In C, the data is in nested structs, with mixed types, floats, ints, char arrays, int arrays, variable length arrays of structs etc. My preference would be to access the data in a similar fashion to C, casting the byte array of received...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.