473,387 Members | 1,669 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.

Question on user defined conversions

I have 2 classes A and B. I want to write conversion routines to convert from A to B and vice versa. I think it can be done through conversion constructor in one class and conversion operator in other. But on compilation I get the following error
error C2027: use of undefined type B
Probably this is because I am using B in in class A before defining it. But I move class B above A in code I get same error
error C2027: use of undefined type A
How should I get around this issue?
Apr 5 '11 #1
2 1453
Banfa
9,065 Expert Mod 8TB
Firstly you should post your code if you are getting errors for us to be able to provide proper help.

However this is a common issue. I expect that you have defined you class methods in the class definition (in the .h file) rather than just declared the class methods in the class definition (in the .h file) and then defined the class methods in a separate .cpp file.

The problem is that you can't access a class until you have seen its definition but if the definition of class A requires the definition of class B and the definition of class B requires the definition of class A then yoiu get stuck because both A and B require B and A to be defined first which is not possible.

Example (wont compile
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     int GetData()
  5.     {
  6.         return data;
  7.     }
  8.  
  9.     void CopyB(B& b)
  10.     {
  11.         data = b.GetData();
  12.     }
  13.  
  14. private:
  15.     int data;
  16. };
  17.  
  18. class B
  19. {
  20. public:
  21.     int GetData()
  22.     {
  23.         return data;
  24.     }
  25.  
  26.     void CopyA(A& a)
  27.     {
  28.         data = a.GetData();
  29.     }
  30.  
  31. private:
  32.     int data;
  33. };
  34.  
Produces: bytes.cpp:9: error: 'B' has not been declared

However normally you only require the definition of a class where you access its members and that is only inside the methods. If you move the definition of the methods out of the classes then you get

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     int GetData();
  5.     void CopyB(B& b);
  6.  
  7. private:
  8.     int data;
  9. };
  10.  
  11. class B
  12. {
  13. public:
  14.     int GetData();
  15.     void CopyA(A& a);
  16.  
  17. private:
  18.     int data;
  19. };
  20.  
Compile that and you get the same error as before but now the definition of class A only uses a reference to class B and you can use a forward declaration to class B like this

Expand|Select|Wrap|Line Numbers
  1. class B; //  <----- Forward declaration
  2.  
  3. class A
  4. {
  5. public:
  6.     int GetData();
  7.     void CopyB(B& b);
  8.  
  9. private:
  10.     int data;
  11. };
  12.  
  13. class B
  14. {
  15. public:
  16.     int GetData();
  17.     void CopyA(A& a);
  18.  
  19. private:
  20.     int data;
  21. };
The forward declaration tells the compiler that B is a class name and provides enough information for the compiler to create pointers and references to class B, since for the class definition all you need is a reference that is enough to allow the definition of class A to compile and that allow the following definition of class B to compile.

You then put the definitions of the methods after both the definitions of the classes

Expand|Select|Wrap|Line Numbers
  1. int A::GetData()
  2. {
  3.     return data;
  4. }
  5.  
  6. void A::CopyB(B& b)
  7. {
  8.     data = b.GetData();
  9. }
  10.  
  11. int B::GetData()
  12. {
  13.     return data;
  14. }
  15.  
  16. void B::CopyA(A& a)
  17. {
  18.     data = a.GetData();
  19. }
  20.  
You always get this problem when you have a circular reference of classes and the solution is always to put the definitions of the methods into separate files, make sure the class definitions only use references or pointers to the other classes and use forward declarations to resolve the issues.
Apr 5 '11 #2
Thanks a lot. The forward declaration in the header file worked
Apr 6 '11 #3

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

Similar topics

13
by: dawatson833 | last post by:
I have several stored procedures with parameters that are defined with user defined data types. The time it takes to run the procedures can take 10 - 50 seconds depending on the procedure. If I...
6
by: Ken Post | last post by:
Hi All: I've read a whole slew of posts about creating temp tables using stored proceedures to get the crosstab ability, but I'm wondering if, for this specific case, there might be a more...
1
by: JC Mugs | last post by:
Does anyone know how to set user defined paper sizes for and Epson LQ570E Printer under Windows XP Prof? The driver does not allow for user defined paper sizes.. So setting them from a report...
1
by: Patric_J | last post by:
Why can't the as operator perform user-defined conversions? Is it a C# problem or with MSIL? I can't see any reasons why the compiler should'nt be able to use a user-defined conversion operator....
0
by: Aaron Queenan | last post by:
ECMA 334 section 13.4.3 User-defined implicit conversions states that D, the set of types from which user-defined conversion operators will be considered, includes (among other things) the base...
3
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100...
2
by: eastern_strider | last post by:
I'm running into problems about defining a comparison function for a map which has a user defined key. For example: class Key { public: string name; int number; Key (na, nu) : name (na),...
3
by: Wayne | last post by:
Are user-defined conversions chosen at compile time, or are there ways to make sure that they are chosen at run-time, based on the actual type of the object? Here is a simplified example of what...
6
by: p4willi | last post by:
I've defined two arrays in a Module called PubVars Public Type LinesRec CORRECT As Integer QUESTION As String PROC As Integer PAY As Integer End Type Public Lines_Array() As...
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
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
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
jinu1996
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 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.