![]() |
|
#1
|
|||
|
|||
Problem accessing functions in inherited classesHi. I'm trying to write a program to create a list of various objects (all of which are inherited from the same base class) in a list<> container, and then go through the list, accessing a 'Show()' function defined in each object's class. A (stripped-down) version of my code is below:
Shape.h: CPP / C++ / C Code:
and shape.cpp: CPP / C++ / C Code:
The code compiles ok but when it is executed I get the error message: Code:
as soon as the program attempts to access the Show function (btw I'm compiling and executing using xcode on Mac OS X). I'va also tried declaring Show as a pure virtual function CPP / C++ / C Code:
Code:
As you'll probably be able to see from the code, I'm still quite new to C++ so this is probably a really simple problem to fix but it's really got me stumped. If anyone knows what I've done wrong, please let me know?? Thanks. |
|
#2
|
|||
|
|||
Re: Problem accessing functions in inherited classesQuote:
You are pushing a pointer onto the list. The thing that it points to goes out of scope at the end of that block. Therefore the program crashes when Show() tries to access that object. You could try something like: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Problem accessing functions in inherited classesThanks for your help Dave.
I've made the change you suggested and (unsurprisingly!) it works. One more question though: I've now dynamically allocated all the objects I create in the createList() function using new, I then push pointers to these objects onto a list<Shape*>. How can I delete the objects I've created when I'm finnished with them? If I use delete on the object inside the switch statement I won't be able to access the elements in the list from main(), and I can't figure out a way to delete the objects from the main() function after I've finished with them. I've tried adding the line: CPP / C++ / C Code:
I'm sure this is another simple problem to solve if you know what you're doing, any ideas? Thanks again for your help! |
|
#4
|
|||
|
|||
Re: Problem accessing functions in inherited classesQuote:
In other words, any time you are going to remove a member from your list of pointers (with the erase() member function), use delete on that item. Regards, Dave |
|
#5
|
|||
|
|||
Re: Problem accessing functions in inherited classesProblem solved!
Thanks again for your help Dave! |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with using classes as Parameters and Return Types | wmbest2 | C++ Forum | 3 | 13-Mar-2007 21:08 |
| Windows C++ Problem with Class and Functions | Alloishus | C++ Forum | 3 | 28-Sep-2006 19:34 |
| Macro's in functions problem | Tomb332 | C++ Forum | 1 | 23-Jul-2006 15:43 |
| Working with Functions and have a problem answering | jenmaz | C Programming Language | 1 | 23-Nov-2004 02:34 |
| Having problem in calling functions inthe main | harsha | C Programming Language | 1 | 13-Oct-2004 01:05 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The