473,545 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When using a const struct field in an inialization I get an error

11 New Member
Hi,

Hi, I have a typedefed struct, and later when I declare multiple const structures and want to use a field of one in an inialization, I get the following error: "error C2099: initializer is not a constant".

typedef struct {
int Start;
int attribute;
} MyStructure ;

#define FIRST 1

const MyStructure STRUCT0={FIRST, 0} ;
const MyStructure STRUCT1={STRUCT 0.Start+5, 25} ;
const MyStructure STRUCT2={STRUCT 1.Start+35, 175} ;

Any idea about to solve this? It's a c program, and I'm compiling it under Visual Studio 2008.

I don't want to remove the "#define" because it allows me to change multiple structures at once and is more clear conceptually (actuall sometimes FIRST can be a different number), and also I don't want to use the definition in the second, third and following structs declarations because I have multiple defines that are used, and the expressions will become huge as I have more than 20 of this types of structs.

TIA & Regards ...
Feb 19 '08 #1
4 1542
weaknessforcats
9,208 Recognized Expert Moderator Expert
Compiles and links using Visual Studio.NET 2005.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. typedef struct {
  3. int Start;
  4. int attribute;
  5. } MyStructure ;
  6.  
  7. #define FIRST 1
  8.  
  9. const MyStructure STRUCT0={FIRST, 0} ;
  10. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  11. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  12. int main()
  13. {
  14.  
  15. }
  16.  
Feb 19 '08 #2
Adrian20XX
11 New Member
Compiles and links using Visual Studio.NET 2005.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. typedef struct {
  3. int Start;
  4. int attribute;
  5. } MyStructure ;
  6.  
  7. #define FIRST 1
  8.  
  9. const MyStructure STRUCT0={FIRST, 0} ;
  10. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  11. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  12. int main()
  13. {
  14.  
  15. }
  16.  
It also compiles as a cpp file under Visual Studio 2008, but as my whole program is a C program, I didn't wanted it to be restricted to be a C++ program (i.e. to not compile under a pure C compiler).

So, any aditional idea?

TIA & regards ...
Feb 19 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Then you have to write C.

C++ is not C so C++ code often does not translate to C. Also, some C syntax and rules are C++ compile errors. You can't have it both ways.

However, the C solution is:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. int Start;
  3. int attribute;
  4. } MyStructure ;
  5.  
  6. #define FIRST 1
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12. const MyStructure STRUCT0={FIRST, 0} ;
  13. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  14. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  15.  
  16. }
  17.  
You have to initialize your struct variables inside main().

Doing it outside of main() requires a constructor, which is not present in C. Since you don't have a constructor, the compiler provides one that initializes each member of the struct using the values between the braces. If you change your struct to a class and make the members public, it will still compile in C++.
Feb 20 '08 #4
Adrian20XX
11 New Member
Then you have to write C.

C++ is not C so C++ code often does not translate to C. Also, some C syntax and rules are C++ compile errors. You can't have it both ways.

However, the C solution is:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2. int Start;
  3. int attribute;
  4. } MyStructure ;
  5.  
  6. #define FIRST 1
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12. const MyStructure STRUCT0={FIRST, 0} ;
  13. const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
  14. const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
  15.  
  16. }
  17.  
You have to initialize your struct variables inside main().

Doing it outside of main() requires a constructor, which is not present in C. Since you don't have a constructor, the compiler provides one that initializes each member of the struct using the values between the braces. If you change your struct to a class and make the members public, it will still compile in C++.
Thanks for the suggestions weaknessforcats , this has two problems also, but also gave me an idea.

One of the problems is that these constants are used everywhere, so they either have to have global visibility or be passed everywhere as a parameter. And the second one is that I also have to super-optimize my program for speed, so passing them as a parameter is a negative, and also the fact that they have to be initialized dinamically and so these prevents a lot of constants calculations that is made with them to be done in the compilation.

I can try to declare them as global variables (not constants), and see how much is the performance hit I get.

For now I'm using (and perhaps abusing) of the ## operator, and have tons of constants like this:
const MyStructure STRUCT0={FIRST, 0} ;
const MyStructure STRUCT1={STRUCT 0.Start+5, 25} ;
const MyStructure STRUCT2={STRUCT 1.Start+35, 175} ;

#define STRUCT0_START FIRST
#define STRUCT0_ATTRIBU TE 0
#define STRUCT1_START (STRUCT0_START+ 5)
#define STRUCT1_ATTRIBU TE 25
#define STRUCT2_START (STRUCT1_START+ 35)
#define STRUCT2_ATTRIBU TE 175

And then I access them by:
#define MyStructGetStar t(number) STRUCT##number# #_START
#define MyStructGetAttr ibute(number) STRUCT##number# #_ATTRIBUTE

I'll see if I can come with something better, any additional idea is appreciated.

TIA & Regards ...
Feb 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
3338
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does anybody know how I could do this?
9
5364
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1; Const struct Type2 *Field2;
4
2480
by: QQ | last post by:
Hello I am a newbie on network programming. I am trying to receive a packet if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct sockaddr*)&register_addr, &addr_len))==-1){ fprintf(stderr, "error in recvfrom.\n"); exit(1); } The packet I am receiving has the following possible structure.
4
11331
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error LNK2020: unresolved token (0A000007) memset LINK : error LNK2020: unresolved token (0A000008) free LINK : error LNK2020: unresolved token (0A00000A)...
0
1177
by: Sushma | last post by:
Hi All I'm porting my application from VC6 to VC7. There is a COM exe that has successfully registered itself with system. But when I'm using its services in other Dll I get this error. SiIdSearch error LNK2019: unresolved external symbol "public: __thiscall _com_ptr_t<class _com_IIID<struct SIPROGRESSDIALOGMODLib::IProgressDialog,&struct...
13
9639
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
9
3850
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the vargraphic input parameters along the following lines in i_vargraphic100 vargraphic(100) and they are populated from String's in java.
2
2420
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator internally relies on the at function of the superclass. #include <vector>
0
2180
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below)....
0
7815
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...
0
7763
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...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5340
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...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3458
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...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
712
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...

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.