开发者

String/char arrays inside struct

I am trying to create a struct that has multiple string arrays inside of it. For my purposes I wanted to use std::string arrays but char * arrays would also work if they can get the job done. Either way I can't figure out how to initialize things. This is what I have:

initialize.h

#include <string>

struct myStruct
{
    std::string x[22];
    std::string y[8];
};

extern myStruct data[22];

myform.cpp

#include <initialize.h>
#include <string>

myStruct data[22];

data[0].x = {"a", "b", "c", "d", ...};

I am getting errors that look like this:

Error 1 error C2059: syntax error : '{' Error 2 error C2143: syntax error : missing ';' before '{' Error 3 error C2143: syntax error : missing ';' before '}'

I have tried various permutations with char * arrays or std::string * arrays instead but to no avail, I am quite stuck. Did I forget something fundamental开发者_运维问答?

Thanks in advance.


You cannot use the { } array initialization syntax to assign values to arrays. It can only be used when initializing the array right after definition:

int a[3] = { 3, 4, 5 };

but not

int a[3];
a = { 3, 4, 5 }; //error

you will not get around a loop or a manual initialization of every member. But I think the new C++0x standard improves upon this and makes this (and even more initializer syntax) possible.


myStruct data[22];

With the above statement, you have already created 22 objects of type myStruct and each object having it's own x,y string arrays whose size is 22,8 respectively.

You can only initialize each member of an array directly only while declaration and not during assignment operation. So, you can try -

data[0].x[0] = "a";
data[0].x[1] = "b";

// ....

What the error you are doing is something similar to -

int a[5] ;
a = { 1,2,3,4,5 } ; // Error.

int a[] = { 1,2,3,4,5 } ; // Correct.


The

  {"a", "b", "c", "d", ...};

syntax is only allowed when defining a variable, so you could do

  std::string data[4] = {"a", "b", "c", "d"};  // syntax allowed for definition

but the line

  data[0].x = {"a", "b", "c", "d", ...}; // not definition

is not a definition (data[0].x isn't a new variable). Moreover, since this is not a definition, you can't actually place this code outside of a function.

Somewhere in code you're going to have to manually assign each variable (as @Mahesh's answer suggests)


It's not possible to initialize non static array members in C++. Sorry.


If you make these char * instead of strings, you could get away with a static initializer. It's going to be long and ugly though.

struct myStruct
{
    char * x[22];
    char * y[8];
};

extern myStruct data[22];

myStruct data[22] = {
    { // data[0]
        { "a", "b", "c", ... "v" }, // data[0].x
        { "0", "1", ... "7" } // data[0].y
    },
    { // data[1]
    ...
};


The other comments are correct but I believe there is one other thing you can do. You can initialize the structure when you declare it in your header:

struct myStr
{
  string x[22];
  string y[8];
} data[22] = { {...}, {...}, ... };

This too will be long and ugly but might address your question. As others have said, you can't extern this and assign to it after instantiation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜