GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / 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 01-Oct-2006, 02:25
zdenek zdenek is offline
New Member
 
Join Date: Oct 2006
Location: In a pub.
Posts: 2
zdenek is on a distinguished road

Generic searching through vectors


Hi guys,
Ive spent the last four hours working on this... time to turn to the forum.
This query is really complicated... it'l take time to work through.
Thanks very much to anyone that tries.

I'm using STL and SDL.
I have a base class called Shapes.
A number of shapes inherit this base class... eg square, circle etc...

In my main program, i create vectors of all the specific shapes.
i need to do a search to find the co-ordinates of a shape.

how do you create a generic searching algorithm, that scans
through any vector, regardless of the class/object/type the vector
holds, and checks the co-ordinates of the shape?

god i hope this makes sense.

ofcourse, im only searching through shapes, thus all the vectors
have the base class Shape. I cant seem to pass a specific shape vector and catch it as a generic shape vector. the error i cant get around is instantiating
an abstract class Shape.... code to follow:

CPP / C++ / C Code:
// part of the base class - note the pure virtual function virt_drawShape...
// this is the root of the problem it would seem.
class Shapes {
public:
	Shapes(SDL_Surface* scrn);
	void setPos(int x1, int y1);
	virtual void virt_drawShape() = 0;
	int getX1();
	int getX2();
	int getY1();
	int getY2();
		
};

// in main program....
// some possible shape vectors
vector<ProcessShape> processVec;
vector<ProcessShape>::iterator processVecIter;
vector<DecisionShape> decisionVec;
vector<DecisionShape>::iterator decisionVecIter;
vector<StartStopShape> startstopVec;
vector<StartStopShape>::iterator startstopVecIter;

// The way i want to do the search...
// this method returns a general shape object pointer but checks
// through the specific vector shapes.
Shapes* search(int x, int y)
{
// Create a generic shape.
Shapes* obj_found = NULL;

             // if the generic shape is no longer null... return it
             // this is where i go through all the different shape types.
	if (obj_found != checkVec(x,y, decisionVec))
		return obj_found;
	else if (obj_found != checkVec(x,y, startstopVec))
		return obj_found;
	else return obj_found;
}

// its this method that does not work...
// this method checks each shape in a vector, and compares co-ordinates.
Shapes* checkVec(int x, int y, const vector<Shapes>& shapeVec)
{
	// create a generic shape vector
             Shapes* obj_found = NULL;	
	vector<Shapes>::iterator it;
             // loop through all the shapes in the vector...
	for ( ; it != _ptr_shapeVec->end(); ++it)
	{
		// if the co-ordiantes match... remember where...
                          if ((x == it->getX1()) & (y == it->getY1()))
		{
                                 obj_found = shapeVec(it);
		}
	}
	return obj_found;
}


so where have i gone wrong... well i know... i cant create an object
thats pure virtual... but how do i get around that without writing a
search method for every single vector?
  #2  
Old 10-Oct-2006, 08:44
zdenek zdenek is offline
New Member
 
Join Date: Oct 2006
Location: In a pub.
Posts: 2
zdenek is on a distinguished road
Lightbulb

Re: Generic searching through vectors


It came to me in a blinding flashing light - which was infact
a cars headlights when i fell asleep behind the wheel after
getting no sleep trying to finish the program by the deadline.

For those who cared... here's the simple solution...

Don't create multiple vectors of different derived classess....
Instead create one vector of pointers of the base class...

vector<Shapes*> allshapes_ptr;

then each time u make a new shape... just create a new pointer
of that particular shape and push it onto the generic vector list...

Square* square_ptr = new Square;
allshapes_ptr.pushback(square_ptr);

now searching only needs to go through one vector type.
i rock.
 
 

Recent GIDBlogLast Week of IA Training 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
how to commute values of vectors cpit CPP / C++ Forum 4 23-Jun-2006 08:27
vectors of references mirizar CPP / C++ Forum 1 12-Apr-2005 02:02
new domain searching tool. johnpmccann Websites Reviewed Forum 1 06-Jun-2004 10:37
Declaring a vector of vectors? Lethal411 CPP / C++ Forum 2 20-Mar-2004 09:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

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


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