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

Forward declaration usage/problem

Pointer types can be used without error by fwd declarations.
In following code,.
Is there any way to use A::print() inside B::B() ?
===========================================
Expand|Select|Wrap|Line Numbers
  1. struct A;
  2. struct B {
  3.   B(A *pA) {
  4.     cout<<"pA = "<<pA<<endl;
  5.     cout<<"pA->print() = "<<pA->print()<<endl; // throwing error
  6.   }
  7. };
  8. struct A {
  9.   void print() { cout<<"Hello World\n"; }
  10. };
  11. int main()
  12. {
  13.   B obj(new A);
  14. }
  15.  
===========================================
Error:
In constructor `B::B(A*)':
error: invalid use of undefined type `struct A'
error: forward declaration of `struct A'
Jan 27 '10 #1
3 1924
Banfa
9,065 Expert Mod 8TB
The problem is that you are trying to use the structure A at line 5 before it has been defined. At that point the compiler has only seen a forward declaration and that is only good for declaring pointers and references to the structure not for using it.

You can easily use A::print() inside B::B(A *pA) by only declaring B::B(A *pA) in the declaration of B and putting the definition of B::B(A *pA) later after A has been declared.

Expand|Select|Wrap|Line Numbers
  1. struct A;    // Forward declare A
  2.  
  3. struct B {  // Declare B
  4.  
  5.   B(A *pA); // Declare B:B(A *pA)
  6. };
  7.  
  8. struct A {  // Declare A
  9.   void print() { cout<<"Hello World\n"; }
  10. };
  11.  
  12. // Define B::B(A *pA) in the presence of the declaration of A
  13. B::B(A *pA) {
  14.       ....
  15. }
  16.  

BTW Please remeber to put [code] ... [/code] tags round your code.
Jan 27 '10 #2
Thanks Banfa.
Actually later, I had used the similar technique only. It seems that, by language parsing rules, we can't use anything other than pointers.

My code of struct A and struct B were in 2 different header files.
Later on I moved the definitions to the .cpp file to solve it.
Jan 27 '10 #3
Banfa
9,065 Expert Mod 8TB
You are not restricted to pointers (or references), as long as A references B but B does not reference A then A can contain a nested B simply by including B header file

Expand|Select|Wrap|Line Numbers
  1. // Header a.h
  2. #include "b.h"
  3.  
  4. struct A
  5. {
  6.     int anInt;
  7.     B myB;
  8. };
  9.  
Expand|Select|Wrap|Line Numbers
  1. // b.h
  2. struct B
  3. {
  4.     int anInt;
  5.     float aFloat;
  6. };
  7.  
You are restricted to using pointers or references where A references B and B references A.
Jan 27 '10 #4

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

Similar topics

6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
5
by: Simon Elliott | last post by:
For some time I've been using typedefs for STL containers for reasons outlined in: http://www.gotw.ca/gotw/046.htm However a major downside to this is that you can't forward declare a typedef...
1
by: Zeng Dinghao | last post by:
I encouter this problem. here are the sample files: //--------------- File: Bar.h ------------------------------ class Bar : public BarBase { public: void BarFunc(); // there is no BarFunc in...
11
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
2
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...

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.