Setting Structure Defaults

M

Mike Copeland

I'm trying to create a structure and populate it with default values.
Below is my attempt, but it fails (badly) to compile. What did I do
wrong here? How might I fix this? TIA

#define ERLIMIT 5
struct ERMENU
{
string title;
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};
 
V

Victor Bazarov

I'm trying to create a structure and populate it with default values.
Below is my attempt, but it fails (badly) to compile. What did I do
wrong here? How might I fix this? TIA

#define ERLIMIT 5
struct ERMENU
{
string title;
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};

This is covered by FAQ 5.8. Please read the FAQ before posting.

V
 
M

Mike Copeland

I'm trying to create a structure and populate it with default values.
Below is my attempt, but it fails (badly) to compile. What did I do
wrong here? How might I fix this? TIA

#define ERLIMIT 5
struct ERMENU
{
string title;
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};
This is covered by FAQ 5.8. Please read the FAQ before posting.

I don't understand. The FAQ reference (5.8) has nothing to do with
my query here, and examining the FAQ doesn't lead me to a resolution of
my problem. I'm seeking an understanding of how to initialize a
structure element that is declared as basic::string, which is the root
cause of my compile errors.
I can't find anything in the FAQ that deals with this. 8<{{
 
B

Balog Pal

Mike Copeland said:
I'm trying to create a structure and populate it with default
values.
Below is my attempt, but it fails (badly) to compile. What did I do
wrong here? How might I fix this? TIA

#define ERLIMIT 5
struct ERMENU
{
string title;
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};
This is covered by FAQ 5.8. Please read the FAQ before posting.

I don't understand. The FAQ reference (5.8) has nothing to do with
my query here, and examining the FAQ doesn't lead me to a resolution of
my problem. I'm seeking an understanding of how to initialize a
structure element that is declared as basic::string, which is the root
cause of my compile errors.
I can't find anything in the FAQ that deals with this.

I bet there is a section about how-to-post-code, that you clearly did not
follow. :-(
Anyway,

#include <string>

struct S
{
// const char * s;
std::string s;
int i;
} o[2] =
{
{ "a", 1 },
{ "b", 2 },
};

complies fine in cameau, so the best bet is that you messed up somewhere
else.

And your code only get complaints on WORD and LONROM too.
 
B

Bo Persson

Mike said:
I'm trying to create a structure and populate it with default
values. Below is my attempt, but it fails (badly) to compile.
What did I do wrong here? How might I fix this? TIA

#define ERLIMIT 5
struct ERMENU
{
string title;
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};
This is covered by FAQ 5.8. Please read the FAQ before posting.

I don't understand. The FAQ reference (5.8) has nothing to do
with my query here, and examining the FAQ doesn't lead me to a
resolution of my problem. I'm seeking an understanding of how to
initialize a structure element that is declared as basic::string,
which is the root cause of my compile errors.
I can't find anything in the FAQ that deals with this. 8<{{

It's a meta reference - One of the points of 5.8 is that you should
tell us WHAT message you get from the compiler.

And you just did!


Bo Persson
 
M

Mike Copeland

Mike said:
On 8/6/2011 2:33 PM, Mike Copeland wrote:
I'm trying to create a structure and populate it with default
values. Below is my attempt, but it fails (badly) to compile.
What did I do wrong here? How might I fix this? TIA

#define ERLIMIT 5
struct ERMENU
{
string title;
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};

This is covered by FAQ 5.8. Please read the FAQ before posting.

I don't understand. The FAQ reference (5.8) has nothing to do
with my query here, and examining the FAQ doesn't lead me to a
resolution of my problem. I'm seeking an understanding of how to
initialize a structure element that is declared as basic::string,
which is the root cause of my compile errors.
I can't find anything in the FAQ that deals with this. 8<{{

It's a meta reference - One of the points of 5.8 is that you should
tell us WHAT message you get from the compiler.

And you just did!

My apologies. I received a maximum number of C2440 errors with this
code (VS6.0 on Windows XP), but I thought posting a compiler/system-
specific error was/is greatly frowned upon here. Also, regarding the
FAQ 5.8, since I couldn't compile this code (which was cut & pasted from
my program), I couldn't post working code. 8<{{
I was only trying to find out how to write the string-initialize
code, and I thought my query was complete and appropriate. <sigh...>
 
G

Geoff

#include <windows.h> /* for definition of WORD */
#define LONORM 1 /* application specific constant? */

#define ERLIMIT 5

struct ERMENU
{
std::string title; /* changed from string to std::string */
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};

Works fine in VS2010.
 
V

Victor Bazarov

[..] I received a maximum number of C2440 errors with this
code (VS6.0 on Windows XP), but I thought posting a compiler/system-
specific error was/is greatly frowned upon here. Also, regarding the
FAQ 5.8, since I couldn't compile this code (which was cut& pasted from
my program), I couldn't post working code. 8<{{
I was only trying to find out how to write the string-initialize
code, and I thought my query was complete and appropriate.<sigh...>

VS6.0 is not Standard compliant by any stretch of the imagination.
Nobody in their right mind uses that compiler any longer. Please find a
better compiler and port to it (unless you really *have* to stick with
that fossil due to some 3rd party dependency, or whatever). AFA
std::string initialization from a string literal (double-quoted stuff in
your program), you will have to use this form:

struct BLAH { std::string s; } blah[] = { std::string("blah") };

because the compiler is so bad that it can't understand that it needs to
use a specific constructor.

V
 
V

Victor Bazarov

#include<windows.h> /* for definition of WORD */
#define LONORM 1 /* application specific constant? */

#define ERLIMIT 5

struct ERMENU
{
std::string title; /* changed from string to std::string */
short xAxis, yAxis;
short titleLen, dataLen;
short linkPrior, linkNext;
short option;
WORD dispAttr;
short erHead, erTail, erCurrent;
} erMenu[ERLIMIT] =
{
{"category1", 2, 4, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category2", 2, 5, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category3", 2, 6, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category4", 2, 7, 9, 1, 0, 0, 0, LONORM, 0, 0, 0},
{"category5", 2, 8, 9, 1, 0, 0, 0, LONORM, 0, 0, 0}
};

Works fine in VS2010.

No shit. Now try it with VS v6.0...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top