By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

pure virtual method

P: n/a
Hi,

I am probably trying to do something really stupid.

There is a class shape:

class Shape
{
public:
Shape();
virtual ~Shape();
....
virtual bool inside(prec_t x, prec_t y) = 0;
....
virtual Shape * copy() = 0;
....
};

and a class circle which is derived from shape

class Circle : public Shape
{
public:
Circle(prec_t radius);
Circle(prec_t radius, prec_t outerRadius);
Circle();
virtual ~Circle();

....
bool inside(prec_t x, prec_t y);
....
Shape * copy();
....
};

ok, now the class Surface has a member of type Shape *:

class Surface
{
public:
....
Shape* myShape;
....
}
finally the last class derives from Surface:
class EvenASph : public Surface
{
public:
....
Surface outerShell;
Surface lowerShell;
....
}

now in this class I am trying to do something like

1) upperShell.shape = this->shape->copy();
2) lowerShell.shape = this->shape->copy();

this seems to work, the compiler does not complain.
But I occasionally encountered crashes when I call
upperShell.shape->inside()

Occasionally meaning, either it does not work at all -
the program immediately causes a Segmentation fault -
or it just work fine and never crashes.
I used the compiler g++ 4.0.3.
If instead of 1 and 2) I do, for example
lowerShell.shape = this->shape

I always get an error when trying to execute:

pure virtual method called
terminate called without an active exception
Am I trying to do something really illegal?

Thanks,

Maximilian

Aug 27 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
mf********@gmail.com wrote:
>
I always get an error when trying to execute:

pure virtual method called
terminate called without an active exception
Am I trying to do something really illegal?
You aren't doing something daft like calling a virtual method from a
constructor are you?

--
Ian Collins.
Aug 27 '07 #2

P: n/a
mf********@gmail.com wrote:
I am probably trying to do something really stupid.
[..]
I always get an error when trying to execute:

pure virtual method called
terminate called without an active exception
Am I trying to do something really illegal?
Calling a pure virtual function is not illegal, it just has
undefined behaviour.

In addition to other's suggestions, check if any of your objects
are passed by value instead of by reference. That would cause
slicing. Basically, put a breakpoint in the constructor of your
abstract base class and see if it's ever called *not* from the
constructor of the derived class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 27 '07 #3

P: n/a
On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.comwrote:
mfabric...@gmail.com wrote:
I always get an error when trying to execute:
pure virtual method called
terminate called without an active exception
Am I trying to do something really illegal?

You aren't doing something daft like calling a virtual method from a
constructor are you?

--
Ian Collins.
Hi all,

by implementing the example, I think I found my mistake.

Thanks for the replies,

Maximilian

Aug 27 '07 #4

P: n/a
mf********@gmail.com wrote:
On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.comwrote:
>mfabric...@gmail.com wrote:
>>I always get an error when trying to execute:
pure virtual method called
terminate called without an active exception
Am I trying to do something really illegal?
You aren't doing something daft like calling a virtual method from a
constructor are you?

Hi all,

by implementing the example, I think I found my mistake.
So, what was the mistake?
Aug 27 '07 #5

P: n/a

<mf********@gmail.comwrote in message...
Hi,
I am probably trying to do something really stupid.
There is a class shape:

class Shape{ public:
Shape();
virtual ~Shape();
...
virtual bool inside(prec_t x, prec_t y) = 0;
...
virtual Shape * copy() = 0;
...
};
You might want to 'define' those pure virtuals for testing.

bool Shape::inside( prec_t x, prec_t y){
std::cerr<<"base virtual \"Shape::inside()\" called"<<std::endl;
return false;
}

Shape* Shape::copy(){
std::cerr<<"base virtual \"Shape::copy()\" called"<<std::endl;
return this;
}

..... because of this:

class Surface{ public: ...
Shape* myShape; // <--------! slice if set to a derived?
}; // ADD the semicolon!!!

>
1) upperShell.shape = this->shape->copy();
There is no 'upperShell' in the incomplete code you showed.
There is no 'shape' in the incomplete code you showed.
( did you mean 'myShape'? )

You also failed to show the definitions for:
Circle::inside()
Circle::copy()

FAQ http://www.parashift.com/c++-faq-lite

--
Bob R
POVrookie
Aug 27 '07 #6

P: n/a
On Aug 27, 4:02 pm, anon <a...@no.nowrote:
mfabric...@gmail.com wrote:
On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.comwrote:
mfabric...@gmail.com wrote:
>I always get an error when trying to execute:
pure virtual method called
terminate called without an active exception
Am I trying to do something really illegal?
You aren't doing something daft like calling a virtual method from a
constructor are you?
Hi all,
by implementing the example, I think I found my mistake.

So, what was the mistake?
Thank you guys for all the very useful suggestions!
Of course in the end is turned out to be my stupidity:
Is said I did

1) upperShell.shape = this->shape->copy();
2) lowerShell.shape = this->shape->copy();

what I did not say is that I deleted the obeject which these two
pointers were pointing two:
delete upperShell.shape; //I guess, this you really
have to do
1) upperShell.shape = this->shape->copy();
delete upperShell.shape; //whereas this was a
mistake, uppershell now should have been
// lowershell.
2) lowerShell.shape = this->shape->copy();
Thanks again everybody!

Maximilian

Aug 28 '07 #7

P: n/a
On 27 Aug, 08:48, mfabric...@gmail.com wrote:
>
class Shape
{
...
virtual Shape * copy() = 0;
...
};

class Circle : public Shape
{
...
Shape * copy();
};
Not directly related to your question,
but in case you didn't know, you could
declare Circle's copy as:
Circle* copy();
This can often eliminate casting from the
calling code. (google for "covariant return
type").

Aug 28 '07 #8

P: n/a
mf********@gmail.com wrote:
On Aug 27, 4:02 pm, anon <a...@no.nowrote:
>mfabric...@gmail.com wrote:
>>On 27 Aug., 10:21, Ian Collins <ian-n...@hotmail.comwrote:
mfabric...@gmail.com wrote:
So, what was the mistake?

Thank you guys for all the very useful suggestions!
Of course in the end is turned out to be my stupidity:
Is said I did

1) upperShell.shape = this->shape->copy();
2) lowerShell.shape = this->shape->copy();

what I did not say is that I deleted the obeject which these two
pointers were pointing two:
delete upperShell.shape; //I guess, this you really
have to do
1) upperShell.shape = this->shape->copy();
delete upperShell.shape; //whereas this was a
mistake, uppershell now should have been
// lowershell.
2) lowerShell.shape = this->shape->copy();
Thats why you need to post minimal compilable source that represents the
problem. This is not even shown on your starting post.
Doing so, you might even find a cause of the problem yourself.
Aug 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.