Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!watnot!watrose!gjditchfield From: gjditchfield@watrose.UUCP Newsgroups: comp.lang.c++ Subject: virtual functions Message-ID: <8325@watrose.UUCP> Date: Thu, 11-Dec-86 22:02:53 EST Article-I.D.: watrose.8325 Posted: Thu Dec 11 22:02:53 1986 Date-Received: Sun, 14-Dec-86 15:18:52 EST Distribution: comp Organization: U of Waterloo, Ontario Lines: 72 C++ seems to be generating code that cc won't accept. Am I doing something wrong? This code was taken from section 7.2.8 of The Book. ------------ #include struct employee { employee* next; char* name; short department; virtual void print(); }; void employee::print() { cout << name << "\t" << department << "\n"; } struct manager:employee { employee* group; short level; void print(); }; void manager::print() { employee::print(); cout << "\tlevel" << level << "\n"; } void f(employee* ll) { for (; ll; ll=ll->next) ll->print(); } main() { employee e; e.name = "j.brown"; e.department = 1234; manager m; m.name = "j.smith"; m.level = 2; m.next = &e; f(&m); } ---------------- When I run it through C++, I get these error messages from cc: "virt.cc", line 22: illegal function "virt.cc", line 22: illegal function where line 22 is "ll->print()" in function f. (The local support people have set things up so that C++ files must have names ending in .cc.) C++'s output, slightly prettified, looks like this: ---------------- #line 1 "virt.cc" /* <> */ // ... #line 20 "virt.cc" int f (_auto_ll )struct employee *_auto_ll ; { for(;_auto_ll ;_auto_ll = _auto_ll -> _employee_next ) #line 22 "virt.cc" (*(((int (*)())(*_auto_ll -> _employee__vptr )))) (*(((int (*)())(*_auto_ll -> _employee__vptr ))))( _auto_ll) ; }; --------------- (I have broken the output for line 22 in two, for clarity.) The expression referring to _employee__vptr seems to be meant to retrieve a pointer to the appropriate print() routine, but I don't see why the expression should be generated twice! I would be delighted to find out that this is all my fault. Comments? -- ------------------------------------------------------------------------------- Glen Ditchfield {watmath,utzoo,ihnp4}!watrose!gjditchfield Dept of Computer Science, U of Waterloo (519) 885-1211 x6658 Waterloo, Ontario, Canada "Flame not, lest thou be singed" - Mr. Protocol