473,941 Members | 3,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

forward declaration related problem

Hello,

I have the following code:

class A
{
public:
A(int i = 0):asInt(i) { }
operator B( ) { return B(asInt); }
private:
int asInt;
};
class B
{
public:
B(int i = 0):bsInt(i) { }
operator A( ) { return A(bsInt); }
private:
int bsInt;
};
int main( )
{ }

When I try to compile it my compiler has this to say:
test.cpp:5: parse error before `('
test.cpp:5: syntax error before `('
test.cpp:7: semicolon missing after declaration of `A'
test.cpp:7: parse error at null character
test.cpp: In method `A::A (int)':
test.cpp:4: class `A' does not have any field named `asInt'
test.cpp: At top level:
test.cpp:7: parse error at null character
The problem is obviously that when the compiler hits the reference to B
in A it doesn't yet know anything about A. So I add this:

class B;

to the very beginning of the file and now my compiler rebuts with: test.cpp: In method `A::operator B ()':
test.cpp:7: return type `class B' is incomplete
test.cpp:7: invalid use of undefined type `class B'
test.cpp:1: forward declaration of `class B'


I understand the issue here, but I'm not sure how to solve it. If
anyone could point me in the right direction I'd really appreciate it.
Thanks in advance.

-exits

Jul 22 '05 #1
4 1675
"exits funnel" <ex*********@NO SPAMyahoo.com> wrote:
Hello,

I have the following code:

class A
{
public:
A(int i = 0):asInt(i) { }
operator B( ) { return B(asInt); }
private:
int asInt;
};
class B
{
public:
B(int i = 0):bsInt(i) { }
operator A( ) { return A(bsInt); }
private:
int bsInt;
};
int main( )
{ }
.... I understand the issue here, but I'm not sure how to solve it. If
anyone could point me in the right direction I'd really appreciate it.
Thanks in advance.


You have defined A's operator B() inline; try moving it outside the class
definition, after class B has already been defined:

--- code---
class B;

class A
{
public:
A(int i = 0) : asInt(i) {}
operator B( );
private:
int asInt;
};

class B
{
public:
B(int i = 0):bsInt(i) { }
operator A( ) { return A(bsInt); }
private:
int bsInt;
};

A::operator B()
{
return B(asInt);
}
--- end of code ---

David F
Jul 22 '05 #2
"exits funnel" <ex*********@NO SPAMyahoo.com> wrote...
I have the following code:

class A
{
public:
A(int i = 0):asInt(i) { }
operator B( ) { return B(asInt); }
private:
int asInt;
};
class B
{
public:
B(int i = 0):bsInt(i) { }
operator A( ) { return A(bsInt); }
private:
int bsInt;
};
int main( )
{ }

When I try to compile it my compiler has this to say:
test.cpp:5: parse error before `('
test.cpp:5: syntax error before `('
test.cpp:7: semicolon missing after declaration of `A'
test.cpp:7: parse error at null character
test.cpp: In method `A::A (int)':
test.cpp:4: class `A' does not have any field named `asInt'
test.cpp: At top level:
test.cpp:7: parse error at null character


The problem is obviously that when the compiler hits the reference to B
in A it doesn't yet know anything about A. So I add this:

class B;

to the very beginning of the file and now my compiler rebuts with:
test.cpp: In method `A::operator B ()':
test.cpp:7: return type `class B' is incomplete
test.cpp:7: invalid use of undefined type `class B'
test.cpp:1: forward declaration of `class B'


I understand the issue here, but I'm not sure how to solve it. If
anyone could point me in the right direction I'd really appreciate it.


Stop putting implementation of the functions in class definition,
move the bodies of 'operator blah()' functions _after_ both A and
B have been defined.

Victor
Jul 22 '05 #3


Victor Bazarov wrote:
"exits funnel" <ex*********@NO SPAMyahoo.com> wrote...
I have the following code:

class A
{
public:
A(int i = 0):asInt(i) { }
operator B( ) { return B(asInt); }
private:
int asInt;
};
class B
{
public:
B(int i = 0):bsInt(i) { }
operator A( ) { return A(bsInt); }
private:
int bsInt;
};
int main( )
{ }

When I try to compile it my compiler has this to say:
test.cpp:5 : parse error before `('
test.cpp:5 : syntax error before `('
test.cpp:7 : semicolon missing after declaration of `A'
test.cpp:7 : parse error at null character
test.cpp: In method `A::A (int)':
test.cpp:4 : class `A' does not have any field named `asInt'
test.cpp: At top level:
test.cpp:7 : parse error at null character


The problem is obviously that when the compiler hits the reference to B
in A it doesn't yet know anything about A. So I add this:

class B;

to the very beginning of the file and now my compiler rebuts with:
test.cpp: In method `A::operator B ()':
test.cpp:7 : return type `class B' is incomplete
test.cpp:7 : invalid use of undefined type `class B'
test.cpp:1 : forward declaration of `class B'


I understand the issue here, but I'm not sure how to solve it. If
anyone could point me in the right direction I'd really appreciate it.

Stop putting implementation of the functions in class definition,
move the bodies of 'operator blah()' functions _after_ both A and
B have been defined.

Victor


Thanks David and Victor, that solved my problem. It's always so easy
once someone shows you the answer :)

-exits

Jul 22 '05 #4
exits funnel <ex*********@NO SPAMyahoo.com> wrote:
class A
{
public:
A(int i = 0):asInt(i) { }
operator B( ) { return B(asInt); }
private:
int asInt;
};
class B
{
public:
B(int i = 0):bsInt(i) { }
operator A( ) { return A(bsInt); }
private:
int bsInt;
};


I'm not sure there is any way to solve that with a forward
declaration. But you can just make B do the conversion in both
directions, and A not do either of the conversions,

class A
{
public:
A(int i = 0):asInt(i) { }
friend class B;
private:
int asInt;
};
class B
{
public:
B(int i = 0):bsInt(i) { }
B(A a):bsInt(a.asIn t) { } // convert A to B
operator A( ) { return A(bsInt); } // convert B to A
private:
int bsInt;
};
int main()
{
A a1(0);
B b2 = a1;
A a3 = b2;
B b4 = a3;
}

--
Dave O'Hearn
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5234
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 TESTDRIVER_HPP #include <ostream> namespace ns_testdriver{ using std::ostream;
6
2861
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error: "field `foo' has incomplete type" for the following code: #include <iostream> #include <vector>
1
2368
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 Barbase }; //----------------------------------------------------------
11
2826
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
3350
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 2 - Changing the constructor of the modeless dialog to accept the parent object:" line and go onwards is what I'm trying to do Basically, there is the main form, and then there's the modeless form. I want the modeless form act like a modal form,...
23
3886
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
1
3314
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 dog's members can access all the cat's private members. // cat.h //
8
28486
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
8361
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
0
10134
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6080
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.