473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

compile problem: friend class w/ non default constructor

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
4 2152
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
10
by: Martin Jørgensen | last post by:
Hi, I got this piece of code, but I won't compile: #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// struct link ...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
0
by: Abhishek Padmanabh | last post by:
I have been trying out boost's serialization library for the past few days. And I have come across a problem serializing a class that has a reference member. The code is posted as below: ...
5
by: mc | last post by:
Hello, I'm trying to create a class (A) with a default constructor friend to another one (B) as follows: class A { public: A(int a); ~A();
14
by: Sweeya | last post by:
/*Program to do manipulations on a string*/ #include <iostream> using namespace std; class String { int len; char *p;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.