GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-May-2007, 15:38
jj90 jj90 is offline
New Member
 
Join Date: May 2007
Posts: 3
jj90 is on a distinguished road

Problem accessing functions in inherited classes


Hi. 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:
//Shape class - base class
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>

class Shape{
	public:
		Shape() {}
        virtual ~Shape()     {}
        virtual float GetArea() = 0;
		virtual void Show() {}
};
#endif

//Circle class - derived from Shape class
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
class Circle : public Shape {
	public:
		Circle(float radius) {_radius = radius;}
        virtual ~Circle()  {}
        virtual float GetArea(){
			_area=3.142*_radius*_radius;
            return _area;
		}
		virtual void Show(){
			std::cout << "CIRCLE" << '\t' << "Radius: " << _radius << '\t' << "Area: " << GetArea() << std::endl;
		}
	protected:
        float _radius;
		float _area;
};
#endif

and shape.cpp:
CPP / C++ / C Code:
#include "Shape.h"
#include <iostream>
#include <list>

using namespace std;

list<Shape*> createList(void){
	list<Shape*> ShapeList;
	int choice;
	cout << endl << "MAKE A LIST OF SHAPES" << endl;
	while(choice!=0){
		cout << endl << "To stop creating the list, press '0'" << endl;
		cout << "Please choose a shape to add to the list..." << endl;
		cout << "Enter '1' for a circle" << endl << endl;
		cin >> choice;
		if (choice==0){
			break;
		}
		switch(choice){
			case 1:	{float radius;
					cout << endl << "You have chosen 'Circle'" << endl;
					cout << "Please enter the radius:" << endl;
					cin >> radius;
					Circle aCircle = Circle(radius);
					//aCircle.Show();
					Shape *pCircle = 0;
					pCircle = &aCircle;
					ShapeList.push_back(pCircle);
					break;
					}
			default:{cerr << "Invalid choice. Please try again." << endl;
					break;
					}
		}
	}
	return ShapeList;
}

int main () {
    list<Shape*> myList = createList();
	
	for(list<Shape*>::const_iterator ci = myList.begin(); ci != myList.end(); ++ci){
		Shape *temp = *ci;
		temp->Show();
	}
	
	return 0;
}

The code compiles ok but when it is executed I get the error message:
Code:
Project has exited due to signal 10 (SIGBUS)

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:
virtual void Show() = 0;
in the base class but the program still crashes at the same point, just with a slightly different error message:
Code:
Project has exited due to signal 11 (SIGSEGV).

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  
Old 14-May-2007, 13:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem accessing functions in inherited classes


Quote:
Originally Posted by jj90
A (stripped-down) version of my code is below:


CPP / C++ / C Code:
		switch(choice){
			case 1:	{float radius;
					cout << endl << "You have chosen 'Circle'" << endl;
					cout << "Please enter the radius:" << endl;
					cin >> radius;
					Circle aCircle = Circle(radius);
					//aCircle.Show();
					Shape *pCircle = 0;
					pCircle = &aCircle;
					ShapeList.push_back(pCircle);
					break;
					}

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:
        switch (choice) {
        case 1:{
                float radius;
                cout << endl << "You have chosen 'Circle'" << endl;
                cout << "Please enter the radius:" << endl;
                cin >> radius;
                Circle *pCircle = new Circle(radius);
                ShapeList.push_back(pCircle);
                break;
            }

Regards,

Dave
  #3  
Old 14-May-2007, 13:49
jj90 jj90 is offline
New Member
 
Join Date: May 2007
Posts: 3
jj90 is on a distinguished road

Re: Problem accessing functions in inherited classes


Thanks 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:
myList.erase(myList.begin(), myList.end());
in main() but I get the feeling that this just empties the list and that the objects I created in the switch statement are still sitting around.

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  
Old 14-May-2007, 14:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem accessing functions in inherited classes


Quote:
Originally Posted by jj90
I'm sure this is another simple problem to solve if you know what you're doing, any ideas?
Before erasing the list (or any time that the list goes out of scope): You traverse the list, using delete on each list member. (The members of the list are pointers, and each pointer value was obtained from the new operator, so each one is a valid argument to the delete operator.)

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  
Old 14-May-2007, 15:12
jj90 jj90 is offline
New Member
 
Join Date: May 2007
Posts: 3
jj90 is on a distinguished road
Smile

Re: Problem accessing functions in inherited classes


Problem solved!

Thanks again for your help Dave!
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 08:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.