473,396 Members | 2,004 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,396 software developers and data experts.

Forward reference compiler error...


Why does the following code cause a compiler error?
class A; // forward reference

class B
{
foo()
{
a = new A;

cout << a->bar(); // compiler error here
}

A* a;
}

class A
{
int bar()
{
return 1;
}
}
The compiler error I get is (note the use of 'struct' not 'class'):

error: invalid use of undefined type 'struct A'
error: forward declaration of 'struct A'
When I place the class B definition after class A, the error goes away. I
thought it was okay to use a "pointer" to an object of a forward-referenced
class.

Jun 27 '08 #1
6 1349
On 2008-05-03 10:03:33 -0400, "barcaroller" <ba*********@music.netsaid:
>
Why does the following code cause a compiler error?
class A; // forward reference

class B
{
foo()
{
a = new A;

cout << a->bar(); // compiler error here
}

A* a;
}

class A
{
int bar()
{
return 1;
}
}
The compiler error I get is (note the use of 'struct' not 'class'):

error: invalid use of undefined type 'struct A'
error: forward declaration of 'struct A'
When I place the class B definition after class A, the error goes away. I
thought it was okay to use a "pointer" to an object of a forward-referenced
class.
At the point of "new A" the compiler has to generate code to create an
object of tpye A. Since it hasn't seen the definition of A, it can't do
that.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2

"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2008050310124475249-pete@versatilecodingcom...
>
At the point of "new A" the compiler has to generate code to create an
object of tpye A. Since it hasn't seen the definition of A, it can't do
that.

Thank you for your response. Actually, it's not the new() that is causing
me problems. Let me present the problem in another way.
class A; // forward reference

class B
{
int print()
{

}

foo(A* a)
{
cout << a->print(); // compiler error here
}
}

class A
{
int print()
{

}

bar(B* b)
{
cout << b->print();
}
}

main()
{
a = new A;
b = new B;

b->foo(a);
a->bar(b);
}
Basically, I have two objects that are dependent on each other. I thought I
would be okay as long as I use pointers to these objects. Is there a way
around this problem (other than re-designing)?

Jun 27 '08 #3
On 2008-05-03 10:36:50 -0400, "barcaroller" <ba*********@music.netsaid:
>
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2008050310124475249-pete@versatilecodingcom...
>>
At the point of "new A" the compiler has to generate code to create an
object of tpye A. Since it hasn't seen the definition of A, it can't do
that.


Thank you for your response. Actually, it's not the new() that is causing
me problems. Let me present the problem in another way.
class A; // forward reference

class B
{
int print()
{

}

foo(A* a)
{
cout << a->print(); // compiler error here
}
}
At the point of the call to a->print() the compiler has to generate
code to call a member function of an object of type A. Since it hasn't
seen the definition of A, it can't do that.
>
Basically, I have two objects that are dependent on each other. I thought I
would be okay as long as I use pointers to these objects. Is there a way
around this problem (other than re-designing)?
Yup. Move the definition of B::foo outside the class definition, after
the definition of A.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4
In article <fv**********@aioe.org>, ba*********@music.net says...
Why does the following code cause a compiler error?
class A; // forward reference

class B
{
foo()
{
a = new A;

cout << a->bar(); // compiler error here
}

A* a;
}

class A
{
int bar()
{
return 1;
}
}
Because when you declare (but don't define) the class, it's an
incomplete type. You can create a pointer (or reference) to that type,
but anything that involves _dereferencing_ the pointer (among other
things) needs a _definition_ of the class.

For the compiler to handle something like 'a->b()', it has to have seen
declarations for both 'a' and 'b'. Using a reference would be the same
-- for you to use something like 'x.y()', it has to have seen
declarations of both 'x' and 'y'.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5
barcaroller wrote:
>
When I place the class B definition after class A, the error goes away. I
thought it was okay to use a "pointer" to an object of a forward-referenced
class.
Use? No. It is OK to _declare/define_ a pointer to a forward-declared class. And
it is OK to _use_ that pointer in a number of very very limited ways. "Limited
ways" in this case means that it is not OK to apply a member access operators
('->' in your example) to this pointer, and it is not OK create object of that
forward-declared incomplete type using 'new'.

Move the definitions of the offending inline members to a point _after_ the
definition of class A, and the problem will go away.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #6
barcaroller wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2008050310124475249-pete@versatilecodingcom...
>>
At the point of "new A" the compiler has to generate code to create
an object of tpye A. Since it hasn't seen the definition of A, it
can't do that.


Thank you for your response. Actually, it's not the new() that is
causing me problems. Let me present the problem in another way.
class A; // forward reference

class B
{
int print()
{

}

foo(A* a)
{
cout << a->print(); // compiler error here
}
}

class A
{
int print()
{

}

bar(B* b)
{
cout << b->print();
}
}

main()
{
a = new A;
b = new B;

b->foo(a);
a->bar(b);
}
Basically, I have two objects that are dependent on each other. I
thought I would be okay as long as I use pointers to these objects. Is
there a way around this problem (other than re-designing)?
Ouput of program is
21

#include <iostream>

class A; // forward reference

class B
{
public:
int print()
{
return 1;
}

void foo(A* a);
};

class A
{
public:
int print()
{
return 2;
}

void bar(B* b)
{
std::cout << b->print();
}
};

void B::foo(A* a)
{
std::cout << a->print();
}

main()
{
A* a = new A;
B* b = new B;

b->foo(a);
a->bar(b);

delete a;
delete b;
}


--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #7

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

Similar topics

3
by: Martin Eisenberg | last post by:
Hi! I have a header with two classes, WidthLogger and Hyst. WidthLogger is declared first; its ctor takes a Hyst reference. Hyst declares WidthLogger a friend. Hyst constructs a WidthLogger...
4
by: exits funnel | last post by:
Hello, I have the following code: class A { public: A(int i = 0):asInt(i) { } operator B( ) { return B(asInt); } private:
11
by: Randy Yates | last post by:
I'm having a problem with forward references. For example, class DATE; class MYCLASS; class MYCLASS { public:
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
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? ...
3
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i...
10
by: collection60 | last post by:
Will C++ ever have Java-like multiple level compiling? So that we can dump the old model of having to first declare then write the code? I find the declaration wastes so much time in coding....
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
3
by: subramanian100in | last post by:
Consider the following piece of code stored in a file called x.cpp class Test; extern void fn_one(Test t); extern Test fn_two(); Test fn_three(const Test &obj) { fn_one(obj);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.