Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: initialization with "{...}" expected for aggregate object - c++

struct test
{
    unsigned int test1;
    unsigned char test2[4096];
    unsigned int test3;
} foo

struct foobar
{
unsigned char data[4096];
}

if i want to access the struct, i say foo.test1, foo.test2[4096], etc.. however, when I wish to return the data present in foo.test2 in the following manner

pac.datafoo = foo.test2[4096];

unsigned char data[4096] =  pac.datafoo;

this is the error I get:

error: initialization with "{...}" expected for aggregate object

what is the mistake i'm doing?

like image 565
pistal Avatar asked Feb 21 '14 02:02

pistal


2 Answers

You need to learn the array initialization method. It's NOT simply assigned as the single variable.

Some examples:

int arrayone[3] = {0}; // assign all items with 0

int arraytwo[3] = {1, 2, 3 }; // assign each item with 1, 2 and 3

int arraythree[3]; // assign arraythree with arraytwo
for (int i = 0; i < 3; ++i) {
    arraythree[i] = arraytwo[i];
}
like image 182
Charlie Avatar answered Oct 23 '22 02:10

Charlie


add ";" at the end of the struct.

struct test
{
    unsigned int test1;
    unsigned char test2[4096];
    unsigned int test3;
} foo ;

struct foobar
{
unsigned char data[4096];
} ;
like image 33
Steve Avatar answered Oct 23 '22 03:10

Steve