473,387 Members | 1,516 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 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 1652
"exits funnel" <ex*********@NOSPAMyahoo.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*********@NOSPAMyahoo.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*********@NOSPAMyahoo.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*********@NOSPAMyahoo.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.asInt) { } // 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
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...
6
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:...
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...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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,...
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.