By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

forward declaration related problem

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
"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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.