Why can't I create an instance of A and B in the Class C definition?
#include <iostream>
class B;
class A;
class C {
public:
B b;
A a;
};
class B {
int b;
};
class A {
int a;
};
int main (void)
{
C c;
return 0;
} 10 1327 th********@gmail.com wrote:
Why can't I create an instance of A and B in the Class C definition?
Because class A and B are not yet defined. th********@gmail.com wrote:
Why can't I create an instance of A and B in the Class C definition?
#include <iostream>
class B;
class A;
class C {
public:
B b;
A a;
};
To understand how to construct an instance of C, the compiler needs to
know what the internal structures of 'B' and 'A' are. Since you didn't
define (only declared) the classes 'B' and 'A' before 'C', the compiler
cannot do what it needs. Move the definition of 'C' _after_ 'B' and 'A'
definitions.
>
class B {
int b;
};
class A {
int a;
};
int main (void)
{
C c;
return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Kavya wrote:
th********@gmail.com wrote:
Why can't I create an instance of A and B in the Class C definition?
Because class A and B are not yet defined.
Thanks for the reply,
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
AwooOOoo th********@gmail.com wrote:
Thanks for the reply,
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
AwooOOoo
You cannot put B and C members in A unless B and C are defined first.
One reason is very fundamental: The compiler must know their size in
order to make them part of C.
You can, however, put pointers to B and C in A. The compiler knows what
size a pointer is :)
--
Scott McPhillips [VC++ MVP] th********@gmail.com wrote:
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?
You seem to be asking a question here. The answer is "yes, you do
seem to have thought that".
>How can I have it know about the
definition of A and B whilst still having them listed after C?
"How can I have my cake and eat it too?"
(for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
The closing parenthesis is missing.
This is precisely the point. The compiler has to know the sizes of
the objects A and B so it can generate proper code. This is the
requirement of the language, mostly so that the compilers don't have
to be too sophisticated (which often means unimplementable).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Scott McPhillips [MVP] wrote:
th********@gmail.com wrote:
Thanks for the reply,
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
AwooOOoo
You cannot put B and C members in A unless B and C are defined first.
One reason is very fundamental: The compiler must know their size in
order to make them part of C.
You can, however, put pointers to B and C in A. The compiler knows what
size a pointer is :)
--
Scott McPhillips [VC++ MVP]
Makes Sense. The example I'm playing with is to get familiar with
templates and the calling that I was trying to simplify is better
represented by,
template <class T>
class Balls {
public:
list<Sticks<T*s;
}
template <class T>
class Sticks {
public:
list<Balls<T*b;
}
So I want a list of pointers to the other templated class. I think this
means that I end up with a similar problem to what you described as s
and b are not pointers so the compiler doesn't know the size of the
list inside the class. Any thoughts on how to get around this? The idea
in the mock up would be that each class could contain a list of
pointers to the other class.
Appreciate your thoughts.
AwooOOoo. th********@gmail.com wrote:
>
Kavya wrote:
>th********@gmail.com wrote:
Why can't I create an instance of A and B in the Class C definition?
Because class A and B are not yet defined.
Thanks for the reply,
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
You cannot have that. You will have to break the cycle by using an A* in B
or a B* in A (that is something for which a forwar declaration suffices!).
Think about it:
struct A {
int i;
B b;
};
struct B {
int j;
A a;
};
and ask yourself: will we have sizeof(A) < sizeof(B) because of the b in A
or will we have sizeof(B) < sizeof(A) because of the a in B? Obviously, you
would have both, which is a contradiction. Thus, you cannot do it.
Best
Kai-Uwe Bux
Hi,
Does this help:
class B;
class A
{
B* b;
};
class B
{
A* a;
};
Regards,
Tushar.
Kai-Uwe Bux wrote: th********@gmail.com wrote:
Kavya wrote:
th********@gmail.com wrote:
Why can't I create an instance of A and B in the Class C definition?
Because class A and B are not yet defined.
Thanks for the reply,
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
You cannot have that. You will have to break the cycle by using an A* in B
or a B* in A (that is something for which a forwar declaration suffices!).
Think about it:
struct A {
int i;
B b;
};
struct B {
int j;
A a;
};
and ask yourself: will we have sizeof(A) < sizeof(B) because of the b in A
or will we have sizeof(B) < sizeof(A) because of the a in B? Obviously, you
would have both, which is a contradiction. Thus, you cannot do it.
Best
Kai-Uwe Bux
th********@gmail.com wrote:
Why can't I create an instance of A and B in the Class C definition?
#include <iostream>
class B;
class A;
class C {
public:
B b;
A a;
};
class B {
int b;
};
class A {
int a;
};
int main (void)
{
C c;
return 0;
}
The above is expected unless you use pointers or references to A,B in
class C.
This should still be a minor issue since each class above will probably
end up being declared in its own header and implemented in its own
source file. At least, that is what your goal should be once you have
the skeleton up and running.
As in...
/* ___ c.hpp ___ class C interface declaration */
#ifndef C_HPP_
#define C_HPP_ /* include guard */
#include "a.hpp"
#include "b.hpp"
class C
{
A a;
B b;
public:
C(); // def ctor
};
#endif /* include guard C_HPP_ */
/* ___ c.cpp ___ class C implementation */
#include "c.hpp"
// def ctor
C::C() : a(0), b(0)
{
}
<th********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
Scott McPhillips [MVP] wrote:
>th********@gmail.com wrote:
Thanks for the reply,
I thought that the
class B;
class A;
at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.
AwooOOoo You cannot put B and C members in A unless B and C are defined first. One reason is very fundamental: The compiler must know their size in order to make them part of C.
You can, however, put pointers to B and C in A. The compiler knows what size a pointer is :)
-- Scott McPhillips [VC++ MVP]
Makes Sense. The example I'm playing with is to get familiar with
templates and the calling that I was trying to simplify is better
represented by,
template <class T>
class Balls {
public:
list<Sticks<T*s;
}
template <class T>
class Sticks {
public:
list<Balls<T*b;
}
So I want a list of pointers to the other templated class. I think this
means that I end up with a similar problem to what you described as s
and b are not pointers so the compiler doesn't know the size of the
list inside the class.
No, that's not a problem. You're declaring a list which will hold pointers,
which is fine. The list size is dynamic; it grows when you add items to it.
Is that the actual code you have?
One problem is that you're missing semicolons (;) after the class
definitions. Also, you'd need to forward declare "template <class Tclass
Sticks;" before the Balls class. The it will compile fine.
But how do you try to use this code? What's T?
You could easily have code like this:
Sticks<Balls<int ballsticks;
Balls<Sticks<int stickballs;
Is that the kind of thing you meant to do?
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matt |
last post by:
I'd like to overwrite just one line of a binary file, based on a
position set by seek(). Is there no way to do this? As far as I can
tell I need to read the whole file, change the line, and write...
|
by: Anupam Kapoor |
last post by:
hi all,
a python n00b, so please bear with me. i have a simple question:
i generally name python sources as a-simple-python-example.py. when i
try to import a module named as above, i...
|
by: newgenre |
last post by:
I am using a pre-built package of code for my site, which is called
EasyDisc. All it does is it creates an interactive forum on your site, like
any forum you see anywhere.
I am having a problem...
|
by: Chris Dunaway |
last post by:
I am using the following code to test an .aspx page which has no
presentation and only a handler for the Page_Load event. I am using
this code to POST the contents of an .xml file to the .aspx...
|
by: onefry |
last post by:
Hey I have this prog that i'm working on, starting my first c++ class and kind of a n00b to programming
here it is
#include <iostream>
#include <cstdlib>
using namespace std;
|
by: Charles |
last post by:
I am learning from the Accelerated C++ book. The following example
doesn't work and I don't know why:
#include <iostream>
#include <string>
int main () {
const std::string exclam = "!";
const...
|
by: rtlshred |
last post by:
Hello
I have just, just started C++ programing. the complier I am using is Dev C++
Here is my question:
Once I have written some code, How do I run the program and see the output?
|
by: benwah1983 |
last post by:
Greetings,
Here is my problem:
The following code shows a div with two small nested divs (images with a title), then the div is closed. Another one opens and a "random text" is displayed.
<div...
|
by: ig |
last post by:
First off, I'm a python n00b, so feel free to comment on anything if
I'm doing it "the wrong way." I'm building a discrete event simulation
tool. I wanted to use coroutines. However, I want to know...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
| |