how to declare structure pointer in within a class

Discussion in 'C++' started by sharat, Dec 12, 2006.

  1. sharat

    sharat Guest

    hi all

    can any body tell how to declare the structure pointer within a class.

    i have written the following program but gettin error.

    #include<iostream>

    using namespace std;

    class A
    {

    public :
    struct sample
    {
    int a ;
    int b ;
    };
    struct sample var1;
    struct sample *ptr = &var1;
    };

    int main()
    {

    cout<<"\n struct variable ptr prog\n";
    }

    i m using fc4,gcc compiler.
    i m getting the following error.struct.cpp:15: error: 'a::var1'
    cannot appear in a constant-expression

    struct.cpp:15: error: `&' cannot appear in a constant-expression
    struct.cpp:15: error: ISO C++ forbids initialization of member
    'ptr'
    struct.cpp:15: error: making 'ptr' static
    struct.cpp:15: error: invalid in-class initialization of static data
    member of non-integral type 'a::sample*'

    can any body tell the solution.
    thanx in advance.
    sharat, Dec 12, 2006
    #1
    1. Advertising

  2. sharat

    Guest


    > class A
    > {
    >
    > public :
    > struct sample
    > {
    > int a ;
    > int b ;
    > };
    > struct sample var1;
    > struct sample *ptr = &var1;
    > };
    >


    You can initialize in a constructor, basically the executable
    statement should be under some function.
    , Dec 12, 2006
    #2
    1. Advertising

  3. sharat wrote:
    ....
    > class A
    > {
    >
    > public :
    > struct sample
    > {
    > int a ;
    > int b ;
    > };
    > struct sample var1;
    > struct sample *ptr = &var1;
    > };

    ....

    You initialize members in the constructor... like so.

    class A
    {
    public :
    struct sample
    {
    int a ;
    int b ;
    };
    sample var1;
    sample *ptr;

    A()
    : var1(),
    ptr( & var1 )
    {}
    };
    Gianni Mariani, Dec 12, 2006
    #3
  4. Gianni Mariani wrote:
    > sharat wrote:
    > ...
    >
    >> class A
    >> {
    >>
    >> public :
    >> struct sample
    >> {
    >> int a ;
    >> int b ;
    >> };
    >> struct sample var1;
    >> struct sample *ptr = &var1;
    >> };

    >
    > ...
    >
    > You initialize members in the constructor... like so.
    >
    > class A
    > {
    > public :
    > struct sample
    > {
    > int a ;
    > int b ;
    > };
    > sample var1;
    > sample *ptr;
    >
    > A()
    > : var1(),
    > ptr( & var1 )
    > {}
    > };


    What a load a crap.
    Uenal S. Mutlu, Dec 17, 2006
    #4
  5. sharat

    frame Guest

    Uenal S. Mutlu wrote:

    > What a load a crap.


    Hey! don't break wind???...
    [NOTE: As long as you post this kind of stuff in this group, I shall
    follow up with this advice!]
    frame, Dec 17, 2006
    #5
  6. sharat

    John Carson Guest

    "frame" <> wrote in message
    news:
    > Uenal S. Mutlu wrote:
    >
    >> What a load a crap.

    >
    > Hey! don't break wind???...
    > [NOTE: As long as you post this kind of stuff in this group, I shall
    > follow up with this advice!]



    And then the rest of us will have to read it. Don't feed the trolls. Add
    them to your blocked senders list.

    --
    John Carson
    John Carson, Dec 17, 2006
    #6
    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. mosfet
    Replies:
    1
    Views:
    399
    lallous
    Dec 15, 2003
  2. Bob Smith
    Replies:
    2
    Views:
    2,711
    Jeff Schwab
    Jan 1, 2004
  3. Javi
    Replies:
    2
    Views:
    1,079
  4. Mike

    declare a class pointer

    Mike, May 1, 2010, in forum: C++
    Replies:
    11
    Views:
    499
  5. Replies:
    1
    Views:
    202
Loading...

Share This Page