Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!elroy!spl1!zeek!larry From: larry@zeek.UUCP (Larry Podmolik) Newsgroups: comp.lang.c++ Subject: Inline functions Keywords: What am I doing wrong? Message-ID: <225@zeek.UUCP> Date: 5 Apr 89 23:07:56 GMT Organization: Arthur Andersen & Co., Chicago, IL Lines: 74 I have a question concerning inline functions: where should they be placed? I don't like putting the body of the function in the class declaration, since it makes the class "interface" harder to read, especially if you don't care HOW a routine is implemented. On page 266, Bjarne says: The _inline_ specifier is only a hint to the compiler, does not affect the meaning of the program, and may be ignored. If the inline functions are defined in the class declaration (.h file), then they get included wherever that .h file does. Fine. But say you put them in the corresponding .c file with the "normal" function definitions. Since you are compiling modules separately, how does the compiler figure out how to do inline expansion of the function body? In this case, do they _have_ to be treated as regular functions (i.e., _inline_ ignored)? And if you put them in the .h file, which is included in several modules, and the compiler decides to treat them as regular functions, won't that cause problems when linking (multiple redefinitions of the same function)? This came up when I was adapting the String class example on pp. 184 of Bjarne's book. (AT&T C++ 1.2, 3B2/400 SVR3.0) The stuff on p. 184 was in String.h, except that I removed the function bodies for the operator functions: // String.h // other stuff... class String { /* ... */ friend int operator==(String&, String&); friend int operator==(String&, char*); friend int operator!=(String&, String&); friend int operator!=(String&, char*); }; // end of class String Then in String.c, I put the definitions for these: // String.c #include "String.h" // stuff deleted... inline int operator==(String &x, String &y) { return strcmp(x.p->s, y.p->s) == 0; } inline int operator==(String &x, char *s) { return strcmp(x.p->s, s) == 0; } inline int operator!=(String &x, String &y) { return strcmp(x.p->s, y.p->s) != 0; } inline int operator!=(String &x, char *s) { return strcmp(x.p->s, s) != 0; } This compiled fine, as did the main test program, but I got the following linker errors: CC -o main main.o String.o cc -o main main.o String.o -lC undefined first referenced symbol in file _eqFRCString__PC__ main.o _eqFRCString__RCString___ main.o _neFRCString__RCString___ main.o ld fatal: Symbol referencing errors. No output written to main *** Error code 13 Stop. The same errors happened if I put the inline function definitions at the bottom of String.h. What am I doing wrong?? Any help appreciated. -Larry