forward declaration related problem

E

exits funnel

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::eek:perator 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
 
D

David Fisher

exits funnel said:
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::eek:perator B()
{
return B(asInt);
}
--- end of code ---

David F
 
V

Victor Bazarov

exits funnel said:
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:

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:

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
 
E

exits funnel

Victor said:
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
 
D

Dave O'Hearn

exits funnel said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top