compile problem: friend class w/ non default constructor

Discussion in 'C++' started by ankit_jain_gzb, May 29, 2005.

  1. Hi
    Iam not able to understand why the following code gives compile
    problem.
    Thanks
    Ankit Jain

    class B;

    class A{
    public:
    friend class B;
    B* x;

    A()
    {
    x = new B(3)
    }
    };

    class B{
    public:

    B(int i)
    {
    }
    };

    int main()
    {
    return 0;
    }

    error coming in vc++ (6.0) is --> error C2514: 'B' : class has no
    constructors
     
    ankit_jain_gzb, May 29, 2005
    #1
    1. Advertising

  2. ankit_jain_gzb

    John Carson Guest

    "ankit_jain_gzb" <> wrote in message
    news:
    > Hi
    > Iam not able to understand why the following code gives compile
    > problem.
    > Thanks
    > Ankit Jain
    >
    > class B;
    >
    > class A{
    > public:
    > friend class B;
    > B* x;
    >
    > A()
    > {
    > x = new B(3)
    > }
    > };



    The problem is that at the point at which you define the A constructor, the
    B constructor has not yet been declared.

    > class B{
    > public:
    >
    > B(int i)
    > {
    > }
    > };
    >
    > int main()
    > {
    > return 0;
    > }
    >
    > error coming in vc++ (6.0) is --> error C2514: 'B' : class has no
    > constructors



    Try doing it in this order:

    class B;

    class A{
    public:
    friend class B;
    B* x;
    A();
    };

    class B{
    public:
    B(int i)
    {}
    };

    // Now the B constructor has been declared,
    // we can use it in the A constructor. I declare
    // it inline because functions defined in the
    // class declaration, as per your version,
    // are implicitly inline.

    inline A::A()
    {
    x = new B(3);
    }


    int main()
    {
    return 0;
    }


    --
    John Carson
     
    John Carson, May 29, 2005
    #2
    1. Advertising

  3. ankit_jain_gzb wrote:
    > Hi
    > Iam not able to understand why the following code gives compile
    > problem.
    > Thanks
    > Ankit Jain
    >
    > class B;
    >
    > class A{
    > public:
    > friend class B;
    > B* x;
    >
    > A()
    > {
    > x = new B(3)
    > }
    > };
    >
    > class B{
    > public:
    >
    > B(int i)
    > {
    > }
    > };
    >
    > int main()
    > {
    > return 0;
    > }
    >
    > error coming in vc++ (6.0) is --> error C2514: 'B' : class has no
    > constructors
    >


    Move the class B def so it preceeds the class A def.
    'A' is trying to invoke 'B(int)' which has not yet been
    defined at that point in the source file.

    Larry
     
    Larry I Smith, May 29, 2005
    #3
  4. ankit_jain_gzb

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > class B;
    >
    > class A{
    > public:
    > friend class B;
    > B* x;
    >
    > A()
    > {
    > x = new B(3)
    > }


    Try moving the definition of B before A, as in:

    class B{
    public:

    B(int i) {}
    };

    class A{
    public:
    friend class B;
    B* x;

    A() {
    x = new B(3);
    }
    };

    While you're at it, initialization is generally preferred to
    assignment, giving:

    A() : x(new B(3)) {}

    Since all of A is public, declaring B as its friend isn't
    accomplishing anything (though I suspect this was added in an attempt
    at getting A to see B's ctor).

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, May 29, 2005
    #4
  5. Thank you : John.

    Taking the function(constructor here) declaration outside the class
    really worked.

    Thanks to others too but I could not move the definition of B above A
    because B(the class below A) has reference to A. Sorry I did not show
    in prototype code.
     
    ankit_jain_gzb, May 29, 2005
    #5
    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. desktop
    Replies:
    2
    Views:
    462
    Victor Bazarov
    Jun 6, 2007
  2. Generic Usenet Account
    Replies:
    10
    Views:
    2,245
  3. mc
    Replies:
    3
    Views:
    337
  4. Peter
    Replies:
    2
    Views:
    278
    Öö Tiib
    Jun 6, 2013
  5. Marcel Müller
    Replies:
    2
    Views:
    121
    Marcel Müller
    Mar 15, 2014
Loading...

Share This Page