Mail Archives: djgpp/1998/10/24/15:40:13
On Sat, 24 Oct 1998 08:32:37 -0600, bowman wrote:
>
>
>Mike Ruskai wrote:
>>
>> Well, the first thing I have to say is that your example does not demonstrate
>> the problem you indicate when I try to compile it, with no less than four
>> compilers (IBM VisualAge C++, Borland C++, GNU GCC, and DJGPP).
>
>yeah, but try the non-trivial snippet for interesting errors:
>
><------------------------- badcode.cpp ---------------------->
>#include <iostream.h>
>
>class MyClass {
>public:
> MyClass();
> void say_hi(void);
>protected:
> int data;
>};
>
>void MyClass::say_hi(void) {
> cout << "hello world\n";
>}
>
>
>int main(void)
>{
> MyClass instance(); // point of interest
> instance.say_hi();
>}
>
><--------------------------- end: badcode.cpp
>----------------------->
This is bad code indeed, for you are declaring but not defining the default
constructor.
In addition, you're not declaring the variable correctly. If you're using
the default constructor (which means the constructor that takes no arguments,
whether or not you declared and defined it), you must not use empty
parentheses. I wasn't sure about that, but I've since verified it. When you
use empty parentheses, what you're actually doing is declaring a function
called "instance" that returns a MyClass object.
>or, for another variation on the theme,
>
><--------------------------- stillbad.cpp
>----------------------------->
>
>#include <iostream.h>
>
>class MyClass {
>public:
> MyClass();
> void say_hi(void);
>protected:
> int data;
>};
>
>void MyClass::say_hi(void) {
> cout << "hello world\n";
>}
>
>int main(void)
>{
> MyClass instance; // point of interest
> instance.say_hi();
>}
>
><------------------------------ end: stillbad.cpp
>-------------------------->
This is bad solely because you're declaring a constructor without defining
it. Your linker should give you an unresolved external error.
>this one actually compiles and executes under gcc. I don't know if the
>proir two will
>make it through any compiler, but I would be suspicious of a compiler
>that did accept them.
>
><------------------------------ end: thisworks.cpp
>-------------------------->
>
>
>#include <iostream.h>
>
>class MyClass {
>public:
> MyClass() {};
> void say_hi(void);
>protected:
> int data;
>};
>
>void MyClass::say_hi(void) {
> cout << "hello world\n";
>}
>
>int main(void)
>{
> MyClass instance(); // point of interest
> instance.say_hi();
>}
>
><------------------------------ end: thisworks.cpp
>-------------------------->
This is bad because you're declaring a function instead of defining a
variable.
--
- Mike
Remove 'spambegone' to send e-mail.
- Raw text -