Accessing array member created by another structure constructor

Discussion in 'C++' started by Bubba, Mar 6, 2012.

  1. Bubba

    Bubba Guest

    Greetings to all,

    here's the thing:

    struct foo {
    string a;
    int b;

    foo () {}
    foo (string a, int b);
    };

    struct bar {
    foo *data;
    bar () {
    bar (int m, int n)
    void add (foo S, int i, int j);
    };

    foo::foo (string a, int b) {
    foo::a = a;
    foo::b = b;
    }

    bar::bar (int m, int n) {
    foo* data = new foo[m*n*4];
    }

    Now I have a problem with function add, where I should access particular
    element in data array of foo's and assign values from parameter, but don't
    know exactly how. This doesn't work since it throws segmentation fault:

    bar::add (foo S, int i, int j) {
    data[(i*j*4)-].a = S.a;
    data[(i*j*4)-].b = S.b;
    }

    What's the proper way to do that? I require array of foo's in the bar's
    constructor.

    Best regards,

    --
    "If you lie to the compiler,
    it will get its revenge."
    Henry Spencer
    2.718281828459045235360287471352662497757247093699959574966967627.com
     
    Bubba, Mar 6, 2012
    #1
    1. Advertising

  2. On 3/6/2012 4:43 PM, Bubba wrote:
    > here's the thing:
    >
    > struct foo {
    > string a;
    > int b;
    >
    > foo () {}
    > foo (string a, int b);
    > };
    >
    > struct bar {
    > foo *data;
    > bar () {
    > bar (int m, int n)
    > void add (foo S, int i, int j);
    > };
    >
    > foo::foo (string a, int b) {
    > foo::a = a;
    > foo::b = b;


    See FAQ section 10.

    > }
    >
    > bar::bar (int m, int n) {
    > foo* data = new foo[m*n*4];


    You just declared a local object with the name 'data', which *hid* the
    member with the same name.

    > }
    >
    > Now I have a problem with function add, where I should access particular
    > element in data array of foo's and assign values from parameter, but don't
    > know exactly how. This doesn't work since it throws segmentation fault:
    >
    > bar::add (foo S, int i, int j) {
    > data[(i*j*4)-].a = S.a;
    > data[(i*j*4)-].b = S.b;


    Check the value of 'data' here. And what exactly do the minuses do
    after the closing parentheses in the indexing expressions?

    > }
    >
    > What's the proper way to do that? I require array of foo's in the bar's
    > constructor.


    If you read the FAQ section 10 carefully, you can avoid the mistakes
    like the one you've made.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 6, 2012
    #2
    1. Advertising

  3. Bubba

    Bubba Guest

    Victor Bazarov's log on stardate 06 ožu 2012

    /snip

    Thx, the FAQ helped!

    --
    "If you lie to the compiler,
    it will get its revenge."
    Henry Spencer
    2.718281828459045235360287471352662497757247093699959574966967627.com
     
    Bubba, Mar 9, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Graham
    Replies:
    3
    Views:
    515
    Matt Graham
    Feb 7, 2004
  2. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,644
    jeffc
    May 17, 2004
  3. Neroku
    Replies:
    8
    Views:
    1,215
    Tor Iver Wilhelmsen
    Nov 18, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,253
  5. msimmons
    Replies:
    0
    Views:
    507
    msimmons
    Jul 16, 2009
Loading...

Share This Page