473,385 Members | 1,655 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,385 software developers and data experts.

get the classes under the specified class.

7
Hello. I'm working with classes at the moment. What i wanna do is to get/handle the class B from class A. I red that you can write the name of the class above both classes in this case, as you can do with functions, but the linker writes: "has initializer but incomplete type". I wonder of you can do something similar with vectors aswell.

Expand|Select|Wrap|Line Numbers
  1. class B;
  2.  
  3. class A
  4. {
  5. void get();
  6. };
  7.  
  8. void A::get()
  9. {
  10. B b_class;
  11. b_class.test();
  12. }
  13.  
  14. class B
  15. {
  16. void test();
  17. };
  18.  
  19. void B::test()
  20. {
  21. cout << "test" << endl;
  22. }
  23.  
Dec 27 '07 #1
3 1192
weaknessforcats
9,208 Expert Mod 8TB
class B; <<<<Forward reference

class A
{
void get();
};

void A::get()
{
B b_class; <<<<< Error
b_class.test();
}

class B
{
void test();
};

void B::test()
{
cout << "test" << endl;
}
I made a couple of marks in your code above. First, a class reference is an instruction to the compiler that a class exists even though the compiler has not yet seen the class declaration. Here it says B is a class.

So far, so good.

However in A::get(), there is an object b_class with a type of B. For the compiler to accept this, there must be a class B (there is according to the forward reference) and that class B must have a default constructor (which you can't tell from the forward reference). So you get a compile error.

You say you get a linker error. I doubt that. It's a compile error.

The forward reference is good enough for the compiler to allow a B* but not good enough for the compiler to allow a B object. When you need a B object, then you have to put the class B declaration before the class A declaration.
Dec 27 '07 #2
Saile
7
You mentioned "forward reference", so I searched for it on google and found out this way to solve it:
Expand|Select|Wrap|Line Numbers
  1. class B;
  2.  
  3. class A
  4. {
  5. public:
  6. B *b;
  7. void get();.
  8. };
  9.  
  10. class B
  11. {
  12. void test();
  13. };
  14.  
  15. void B::test()
  16. {
  17. cout << "test" << endl;
  18. }
  19.  
  20. void A::get()
  21. {
  22. b = new B;
  23. b->test();
  24. }
  25.  
And yes I misswrote, I ment compiler error. :)
Dec 27 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Yep. That's the way.
Dec 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: Axel Dahmen | last post by:
I try to combine properties of several classes. This is done by assigning a space separated list of class definitions to an element. However, IE shows a kind of preference when choosing the right...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
4
by: David Ross | last post by:
In the same style-sheet, there are two classes with some properties that have differing values. If I have a CLASS attribute on an element that refers to both classes, how should the conflict be...
3
by: ricky_casson | last post by:
I'm attempting to create a managed C++ class from some an already existing application that I have however some of the classes allow private access via the 'friend' keyword and i've heard that this...
10
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and...
2
by: Jinsong Liu | last post by:
I have a class which serve as a base class. I want to focuses all the classes derived from it implement a interface. Make it a abstract class is not a option since some XML serialization code need...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
0
by: Clive Dixon | last post by:
When working with lots of associated "supporting" classes alongside classes (by this, I mean things such as associated component editor classes specified by , debugger proxy classes specified by ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.