[comp.lang.c++] Inline member functions in .c file

ahmed@athena.mit.edu (Shamim Ahmed) (01/01/91)

Hi!
I am trying to define an inline member function of a class in the
.c file after declaring it in the .h file as follows:

// file A.h
class A
{
 public:
   char *xx();
//.....
};

// file A.c
#include "A.h"

inline char* A::xx()
{
// ....
}

The code compiles, but the linker gives the following error:
ld: Undefined symbol
   A::xx()

What's the problem?

Thanks,

Shamim
 

davidm@uunet.UU.NET (David S. Masterson) (01/03/91)

>>>>> On 31 Dec 90 18:35:00 GMT, ahmed@athena.mit.edu (Shamim Ahmed) said:

Shamim> Hi!
Shamim> I am trying to define an inline member function of a class in the
Shamim> .c file after declaring it in the .h file as follows:

Shamim> // file A.h
Shamim> class A
Shamim> {
Shamim>  public:
Shamim>    char *xx();
Shamim> //.....
Shamim> };

Shamim> // file A.c
Shamim> #include "A.h"

Shamim> inline char* A::xx()
Shamim> {
Shamim> // ....
Shamim> }

A::xx() needs to be declared as inline in A.h or it will be treated as
"outline".  Also, you'll probably want to put the definition of the A::xx() in
the .H file or programs including A.h will not be able to find it (since it's
resolved by CPP).

--
====================================================================
David Masterson					Consilium, Inc.
(415) 691-6311					640 Clyde Ct.
uunet!cimshop!davidm				Mtn. View, CA  94043
====================================================================
"If someone thinks they know what I said, then I didn't say it!"

philip@pescadero.Stanford.EDU (Philip Machanick) (01/03/91)

In article <CIMSHOP!DAVIDM.91Jan2102247@uunet.UU.NET>, cimshop!davidm@uunet.UU.NET (David S. Masterson) writes:
|> A::xx() needs to be declared as inline in A.h or it will be treated as
|> "outline".  Also, you'll probably want to put the definition of the A::xx() in
|> the .H file or programs including A.h will not be able to find it (since it's
|> resolved by CPP).
Not strictly true. As long as the inline declaration is seen by the compiler
before a call, there's no problem. Inlines are expanded statically,
but can appear anywhere in the source. One technique is to have a separate A.i
file, which you include after the A.h - this allows you to make inlines which
reference more than one class, for example. (Not sure what you mean by
"resolved by CPP": presumably, you meant cfront?)
-- 
Philip Machanick
philip@pescadero.stanford.edu