Sign in to follow this  

Untitled

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im having a bit of trouble I have a class called line and a class called arc that are not derived from eachother I am trying to have an array containing both data types. To try this idea out I tried: void* entity; entity=new LINE(); entity->midpoint();***** from this line I get: C:\Documents and Settings\Jay\Desktop\lines.cpp(84) : error C2227: left of '->midpoint' must point to class/struct/union Can anyone shed some light on how I would achieve this? I dont think an abstact base class or derived classes would work since both classes have drastically different fuctions. Thanks in advance for your help

Share this post


Link to post
Share on other sites
You can do something like this:

struct Arc { ... };
struct Line { ... };
typedef union {
Arc arc;
Line line;
} arc_line;
arc_line al[10];
al[0].arc.arcMethod(...);
al[1].line.lineMethod(...);

Share this post


Link to post
Share on other sites
Well you can hack your way round the problem (e.g. using what nmi did) but if they're not derived from a base class and have dratically different functions they probably don't belong in the same array anyway.

However line and arc seem to be pretty similar things, why can't they both be derived from a shape or a shape2d or some other similarly named class?

Share this post


Link to post
Share on other sites
The issue stems from the fact that your pointer is void. This pointer points to some data, but the compiler doesn't know what function you want to call. Sure, you've named it but how many classes might have that same name? You need to cast the pointer to the correct type before you use it.

void* entity;
entity=new LINE();
reinterpret_cast<LINE>(entity)->midpoint()


That should work, but I think the problem lies with your design. You cannot hold two different classes in the same array without hacking a solution or using polymorphism. To do what I've posted you must know, ahead of time, what type each void pointer is pointing to. This is tricky unless you hide them in a pair with an enumeration (integer) and then put that pair in an array (or a vector, which would be the better choice)

Anyway, good luck.

Share this post


Link to post
Share on other sites
Say I derive line and arc from the same base class called entity with midpoint as a virtual function

line has the mamber fuction midpoint while arc does not use this fuction. In my ARC class I do not create a midpoint function. What can I do to avoid an error besides create a 'dummy' midpont fuction in ARC

entity[0]=new LINE();
entity[1]=new ARC();*****gives error below

C:\Documents and Settings\Jay\Desktop\lines.cpp(82) : error C2259: 'ARC' : cannot instantiate abstract class due to following members:
C:\Documents and Settings\Jay\Desktop\lines.cpp(64) : see declaration of 'ARC'

Share this post


Link to post
Share on other sites

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this