Connecting Tech Pros Worldwide Forums | Help | Site Map

compile problem: friend class w/ non default constructor

ankit_jain_gzb
Guest
 
Posts: n/a
#1: Jul 23 '05
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


John Carson
Guest
 
Posts: n/a
#2: Jul 23 '05

re: compile problem: friend class w/ non default constructor


"ankit_jain_gzb" <ankit_jain_gzb@hotmail.com> wrote in message
news:1117334811.921810.9840@g44g2000cwa.googlegrou ps.com[color=blue]
> 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)
> }
> };[/color]


The problem is that at the point at which you define the A constructor, the
B constructor has not yet been declared.
[color=blue]
> class B{
> public:
>
> B(int i)
> {
> }
> };
>
> int main()
> {
> return 0;
> }
>
> error coming in vc++ (6.0) is --> error C2514: 'B' : class has no
> constructors[/color]


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

Larry I Smith
Guest
 
Posts: n/a
#3: Jul 23 '05

re: compile problem: friend class w/ non default constructor


ankit_jain_gzb wrote:[color=blue]
> 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
>[/color]

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
Jerry Coffin
Guest
 
Posts: n/a
#4: Jul 23 '05

re: compile problem: friend class w/ non default constructor


In article <1117334811.921810.9840@g44g2000cwa.googlegroups.c om>,
ankit_jain_gzb@hotmail.com says...

[ ... ]
[color=blue]
> class B;
>
> class A{
> public:
> friend class B;
> B* x;
>
> A()
> {
> x = new B(3)
> }[/color]

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.
ankit_jain_gzb
Guest
 
Posts: n/a
#5: Jul 23 '05

re: compile problem: friend class w/ non default constructor


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.

Closed Thread