forward declaration error

From: Scott (scott_at_nospam.com)
Date: 05/29/04


Date: Sat, 29 May 2004 00:28:25 GMT

Hi All,

I am trying to understand the following errors, and hope someone can help
me along. This is code not originally written by me, but that I am
trying to fix and compile. I have the following in a single file:

#include "A.h"
class foo;

class B : public A
{
public:
        foo * getFooPtr() { return fooObject; }

protected:
        foo * fooObject;
};

template <class T>
int compare( const T * first, const T * second )
{
        foo * object = first->getFoo();
        int number = foo->getNum();
}

It compiles with errors:

error: invalid use of undefined type `struct foo'
error: forward declaration of `struct foo'

I believe this is because the forward declaration of foo doesn't actually
provide any information on what functions are contained within foo, so I
need to change:

class foo;

to

#include "foo.h"

Correct?

However, when I do that, I get:

error: ISO C++ forbids declaration of `foo' with no type
error: `foo' undeclared (first use this function)

The first referring to the use of foo in class B, and the second to the
use of foo in the template function. I am confused, foo is declared in
foo.h, so why the errors? Please help!

Best,
Scott



Relevant Pages

  • Re: Question about multiple files
    ... No. extern just means: The actual definition is somewhere else. ... the compiler can differentiate between declaration and definition just ... > declaration it finds and in this way can I write code expecting it to be ... When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • RE: RFC on first perl script
    ... print $foo; ... Global symbol "$foo" requires explicit package name at - line 4 ... my EXPR: ATTRS ... The "our" declaration has no semantic ...
    (perl.beginners)
  • Re: Weird gcc behaviour with function pointer types
    ... void (struct foo *); ... This looks like a bug in gcc to me -- what do people ... If there is no outer declaration of struct foo visible to both ... Adding the typedef provides a common declaration of struct foo that is ...
    (comp.lang.python)
  • Re: Newbie static class member question [C++]
    ... >and allows subsequent declarations to refer to Foo in certain contexts ... there is a name 'i' which designates an int ... In some contexts a declaration is also a definition. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question regarding prototypes for 0-ary functions.
    ... int fooint x; ... extern int foo(int, ... ... extern int foo(); is not a valid declaration, ... The definition of foo says it takes one parameter, while the declaration ...
    (comp.lang.c)