help in constructor needed

Discussion in 'C++' started by DanielJohnson, Feb 20, 2011.

  1. Hi,

    I am a newbie C++ programmer, so please pardon my ignorance. I am
    creating a class like shown below.

    In the Class B on lines marked by asterisk(*), the compiler complains
    saying "Error: expected a type specifier".

    This is in visual studio compiler on windows. I tried running the same
    on g++ and and g++ complained "error: expected identifier before
    "false""

    Can you please help me ? I am not able to figure out anything form
    these error messages.


    #include<iostream>
    using namespace std;

    namespace A
    {
    class A
    {
    public:
    A()
    {
    std::cout<<"\n default ctor";
    }

    A(bool _d)
    {
    d = _d;
    std::cout<<"\n parametrized ctor";
    }

    private:
    bool d;
    };

    class B
    {
    public:
    // this uses a1 and a2 defined below
    void some_func() { }
    private:
    A a1(false); // This is where compiler is complaining ************
    A a2(true); // This is where compiler is complaining ************
    };
    }
     
    DanielJohnson, Feb 20, 2011
    #1
    1. Advertising

  2. DanielJohnson

    Ian Collins Guest

    On 02/20/11 09:30 PM, DanielJohnson wrote:
    > Hi,
    >
    > I am a newbie C++ programmer, so please pardon my ignorance. I am
    > creating a class like shown below.
    >
    > In the Class B on lines marked by asterisk(*), the compiler complains
    > saying "Error: expected a type specifier".
    >
    > This is in visual studio compiler on windows. I tried running the same
    > on g++ and and g++ complained "error: expected identifier before
    > "false""
    >
    > Can you please help me ? I am not able to figure out anything form
    > these error messages.
    >
    >
    > #include<iostream>
    > using namespace std;
    >
    > namespace A
    > {
    > class A
    > {
    > public:
    > A()
    > {
    > std::cout<<"\n default ctor";
    > }
    >
    > A(bool _d)
    > {
    > d = _d;
    > std::cout<<"\n parametrized ctor";
    > }
    >
    > private:
    > bool d;
    > };
    >
    > class B
    > {
    > public:
    > // this uses a1 and a2 defined below
    > void some_func() { }
    > private:
    > A a1(false); // This is where compiler is complaining ************
    > A a2(true); // This is where compiler is complaining ************


    You can't initialise member variables like that. Initialise them in B's
    constructor.

    B() : a1(false), a2(false) {}

    --
    Ian Collins
     
    Ian Collins, Feb 20, 2011
    #2
    1. Advertising

  3. DanielJohnson

    thomas Guest

    On Feb 20, 4:30 pm, DanielJohnson <> wrote:
    > Hi,
    >
    > I am a newbie C++ programmer, so please pardon my ignorance. I am
    > creating a class like shown below.
    >
    > In the Class B on lines marked by asterisk(*), the compiler complains
    > saying "Error: expected a type specifier".
    >
    > This is in visual studio compiler on windows. I tried running the same
    > on g++ and and g++ complained "error: expected identifier before
    > "false""
    >
    > Can you please help me ? I am not able to figure out anything form
    > these error messages.
    >
    > #include<iostream>
    > using namespace std;
    >
    > namespace A
    > {
    >         class A
    >         {
    >         public:
    >                 A()
    >                 {
    >                         std::cout<<"\n default ctor";
    >                 }
    >
    >                 A(bool _d)
    >                 {
    >                         d = _d;
    >                         std::cout<<"\n parametrized ctor";
    >                 }
    >
    >         private:
    >                 bool d;
    >         };
    >
    >         class B
    >         {
    >         public:
    >                 // this uses a1 and a2 defined below
    >                 void some_func() { }
    >         private:
    >                 A a1(false);  // This is where compiler is complaining ************
    >                 A a2(true);   // This is where compiler is complaining ************
    >         };
    >
    > }
    >
    >


    class A
    {
    int x = 2;
    };

    The compiler will complain because only const static member can be
    initialized in-class.

    The compiler process the code like below:
    1) check each member in class A and allocate memory for each member.
    if the member is const numeric type, the value is stored in symbol
    table and no memory allocated needed.
    if the member is of other type, memory allocation is required for it.
    2) initialize class A with constructor.
     
    thomas, Feb 20, 2011
    #3
  4. DanielJohnson

    Jorgen Grahn Guest

    On Sun, 2011-02-20, DanielJohnson wrote:
    > Hi,
    >
    > I am a newbie C++ programmer, so please pardon my ignorance. I am
    > creating a class like shown below.

    ....
    > class A
    > {
    > public:

    ....
    > A(bool _d) { ...


    Not related to your question, but anyway:

    That should probably be 'explicit A(bool _d)'. Always use 'explicit'
    on such constructors unless you have special reasons not to.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 20, 2011
    #4
  5. Ian Collins <> wrote:
    >> A a1(false); // This is where compiler is complaining ************
    >> A a2(true); // This is where compiler is complaining ************

    >
    > You can't initialise member variables like that.


    Btw, isn't the new standard going to change that?
     
    Juha Nieminen, Feb 20, 2011
    #5
  6. Pete Becker <> wrote:
    > On 2011-02-20 10:38:28 -0500, Juha Nieminen said:
    >
    >> Ian Collins <> wrote:
    >>>> A a1(false); // This is where compiler is complaining ************
    >>>> A a2(true); // This is where compiler is complaining ************
    >>>
    >>> You can't initialise member variables like that.

    >>
    >> Btw, isn't the new standard going to change that?

    >
    > No.


    So this is something else completely?

    http://en.wikipedia.org/wiki/C++0x#Object_construction_improvement
     
    Juha Nieminen, Feb 21, 2011
    #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. Giulio
    Replies:
    9
    Views:
    1,056
    Patrick Kowalzick
    Jun 25, 2003
  2. Mike

    Constructor Help Needed

    Mike, Jul 17, 2006, in forum: Java
    Replies:
    5
    Views:
    2,061
    Mark Space
    Jul 17, 2006
  3. Alfonso Morra
    Replies:
    15
    Views:
    833
    Alfonso Morra
    Aug 12, 2005
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,281
  5. balaji
    Replies:
    3
    Views:
    957
    Jens Thoms Toerring
    Aug 10, 2011
Loading...

Share This Page