By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

compile problem: friend class w/ non default constructor

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"ankit_jain_gzb" <an************@hotmail.com> wrote in message
news:11********************@g44g2000cwa.googlegrou ps.com
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

Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
In article <11********************@g44g2000cwa.googlegroups.c om>,
an************@hotmail.com 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.
Jul 23 '05 #4

P: n/a
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.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.