what is wrong with this?

E

Erdal MUTLU

Hello,

I have problems with this simple program:

#include<iostream>
#include<fstream>
class Point {
public:
int x,y;
Point(int x=0,int y=0)
{ this->x=x; this->y=y; }
~Point() {}
};
class Curve {
public:
virtual int getValue(const double t)=0;
virtual ~Curve();
};


class LineCurve: public Curve {
private:
Point data[2];
double dt;
public:
int getValue(const double dt) { return 0; }
~LineCurve() {}
};

int main(void)
{
Curve* ptr;

ptr=new LineCurve;
return 0;
}


Wenn I comment ptr=new LineCurve; line out, then the code compiles.
Otherweis I get link errors. What is wrong here? It should be something
to do with virtuals, but I couldn't find it out?

Best regards.
Erdal Mutlu
 
K

Karl Heinz Buchegger

Erdal said:
Hello,

I have problems with this simple program:

#include<iostream>
#include<fstream>
class Point {
public:
int x,y;
Point(int x=0,int y=0)
{ this->x=x; this->y=y; }
~Point() {}
};
class Curve {
public:
virtual int getValue(const double t)=0;
virtual ~Curve();
};

class LineCurve: public Curve {
private:
Point data[2];
double dt;
public:
int getValue(const double dt) { return 0; }
~LineCurve() {}
};

int main(void)
{
Curve* ptr;

ptr=new LineCurve;
return 0;
}

Wenn I comment ptr=new LineCurve; line out, then the code compiles.
Otherweis I get link errors.

Well. Usually the linker tells you what's wrong
What is wrong here?

You didn't read the error message.
If I cut&paste your program into VC++, the linker tells me:

error LNK2001: unresolved external symbol "public: virtual __thiscall Curve::~Curve(void)

So it says: Hay buddy. I cannot find an implementation for the destructor of Curve.
Looking up your code, the Code is right. You declared a destructor but nowhere
there is an implementation for it.

Changr to:

class Curve {
public:
virtual int getValue(const double t)=0;
virtual ~Curve() {}
};

^
|
*
 
K

Karl Heinz Buchegger

Karl said:
error LNK2001: unresolved external symbol "public: virtual __thiscall Curve::~Curve(void)

So it says: Hay buddy. I cannot find an implementation for the destructor of Curve.
Looking up your code, the Code is right. You declared a destructor but nowhere

.... the Linker is ....
 
S

Severin Ecker

hi!
error LNK2001: unresolved external symbol "public: virtual __thiscall Curve::~Curve(void)

So it says: Hay buddy. I cannot find an implementation for the destructor of Curve.
Looking up your code, the Code is right. You declared a destructor but nowhere
there is an implementation for it.

i do have a question regarding this. why is an implementation of the
destructor needed, when it's never called anyway? the object is allocated on
free memory but never release with delete, so no destructor is called!? i
think i'm getting something terribly wrong, but what?
thanks.

regards,
sev
 
E

Erdal MUTLU

Karl said:
Well. Usually the linker tells you what's wrong




You didn't read the error message.
If I cut&paste your program into VC++, the linker tells me:

error LNK2001: unresolved external symbol "public: virtual __thiscall Curve::~Curve(void)

So it says: Hay buddy. I cannot find an implementation for the destructor of Curve.
Looking up your code, the Code is right. You declared a destructor but nowhere
there is an implementation for it.

Changr to:

class Curve {
public:
virtual int getValue(const double t)=0;
virtual ~Curve() {}
};

^
|
*

Thank you!

Best regards.
Erdal Mutlu
 
E

Erdal MUTLU

Severin said:
hi!



of Curve.



i do have a question regarding this. why is an implementation of the
destructor needed, when it's never called anyway? the object is allocated on
free memory but never release with delete, so no destructor is called!? i
think i'm getting something terribly wrong, but what?
thanks.

regards,
sev

Hi,

this code is only a part of some bigger class. That is why you do not
see where objects of type Curve are deleted. I had to localize the
problem, that is why the code is somewhat uncomplete.

Best regrads.
Erdal Mutlu
 
K

Karl Heinz Buchegger

Severin said:
hi!


i do have a question regarding this. why is an implementation of the
destructor needed,

Because the compiler has set things up that in the final executable
there has to be a destructor :)
when it's never called anyway? the object is allocated on
free memory but never release with delete, so no destructor is called!?

Hmm. Good question.
The only thing I can think of is:
If you don't define a destructor on your own, then the compiler will
generate one for you. So every class always has a destructor. Since it
absolutely makes no sense to have no destructor at all, it could be
a decission of the compiler writer to set things up such that such
a case is reported.
 
J

JKop

Severin Ecker posted:
i do have a question regarding this. why is an implementation of the
destructor needed, when it's never called anyway? the object is
allocated on free memory but never release with delete, so no
destructor is called!? i think i'm getting something terribly wrong,
but what? thanks.


If the destructor is never to be called, then don't declare it in the class.


As for declaring it in the class and then not defining it, why is this a
problem although even the destructor is never called? Because the C++
Standard says so!


-JKop
 
R

Rob Williscroft

Karl Heinz Buchegger wrote in in
comp.lang.c++:
Because the compiler has set things up that in the final executable
there has to be a destructor :)


Hmm. Good question.

If new throws std::bad_alloc then the destructor will be called,
So the declared destructor is required by the language.



Rob.
 
K

Karl Heinz Buchegger

Rob said:
If new throws std::bad_alloc then the destructor will be called,
So the declared destructor is required by the language.

Thanks for clearification.
 
S

Severin Ecker

Rob said:
If new throws std::bad_alloc then the destructor will be called,
So the declared destructor is required by the language.

thanks for the fast and clear answer!

regards,
sev
 
D

Dave Townsend

You need to provide a body for the ~Curve() destructor. All the
explanations
I've seen for the requirement so far are wrong, you need it because it is
declared
as virtual, that puts an obligation on the class designer to provide a
destructor, whether
or not it is called in the executable.

dave
 
P

Pete Becker

Karl said:
If you don't define a destructor on your own, then the compiler will
generate one for you. So every class always has a destructor. Since it
absolutely makes no sense to have no destructor at all, it could be
a decission of the compiler writer to set things up such that such
a case is reported.

The formalism is 3.2/2: "a virtual member function is used if it is not
pure" and 3.2/3: "Every program shall contain exactly one definition of
every non-inline function or object that is used in that program."
 
K

Karl Heinz Buchegger

Pete said:
The formalism is 3.2/2: "a virtual member function is used if it is not
pure" and 3.2/3: "Every program shall contain exactly one definition of
every non-inline function or object that is used in that program."

Thanks Pete.
Often it is hard to fit all the pieces from the Standard together to be
able to proof something :)
 
P

Pete Becker

Karl said:
Thanks Pete.
Often it is hard to fit all the pieces from the Standard together to be
able to proof something :)

Don't be silly. All it takes is fifteen years of study and two or three
week-long standards meetings a year. <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top