Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vector Troubles
Nick Offline
Senior Member
****

Posts: 271
Joined: Feb 2005
Post: #1
Vector Troubles
I've created a class just to contain some functions and a vector for my handling of objects. I'm posting both the header file and the cpp file. When I compile, I get 15 errors, most being "no match for" types of errors. Does anybody know why this isn't working (I'm new to vectors)? (I've placed (*) on any line that is said to have an error.)

Code:
#ifndef __DRAWLIST__
#define __DRAWLIST__

#include <vector.h>
#include "Object.h"

class SDrawList
{
public:
    void AddObject(CObject &object);
    void RemoveObject(CObject &object);
    void UpdateObject(CObject &object);
    void SortObjects();
    void DrawObjects();

private:
    vector<CObject> objects;
};

#endif


Code:
#include "main.h"
#include "SDrawList.h"


void SDrawList::AddObject(CObject &object)
{
    bool found;
    vector<CObject>::iterator theIterator;
(*)    for (theIterator=objects.begin();theIterator<=objects.back();theIterator++)
    {
(*)        if (objects.at(theIterator) == object) { found = true; }
    }
    if (!found)
    {
        objects.push_back(object);
    }
}

void SDrawList::RemoveObject(CObject &object)
{
    bool found;
    vector<CObject>::iterator theIterator;
    vector<CObject>::iterator delIterator;
(*)    for (theIterator = objects.begin(); theIterator <= objects.back(); theIterator++)
    {
(*)        if (objects.at(theIterator) == object)
        {
            found = true;
(*)            delIterator = objects.at(theIterator);
        }
    }
    if (found)
    {
        objects.erase(delIterator);
    }
}

void SDrawList::UpdateObject(CObject &object)
{
    RemoveObject(object);
    AddObject(object);
}

void SDrawList::SortObjects()
{
    
}

void SDrawList::DrawObjects()
{
    vector<CObject>::iterator theIterator;
(*)    for (theIterator=objects.begin();theIterator<=objects.back();theIterator++)
    {
(*)        objects.at(theIterator).Draw();
    }
}
2004.08.25 04:54 AM
Visit this user's website Find all posts by this user Quote this message in a reply
NCarter Offline
Moderator
*****

Posts: 270
Joined: Feb 2005
Post: #2
Vector Troubles
You're trying to use an iterator as if it were an index. Iterators are actually more like pointers, so you need to do something like this instead:

Code:
vector<CObject>::iterator theIterator;
for (theIterator=objects.begin();theIterator<=objects.back();theIterator++)
{
    if (*theIterator == object) { found = true; }
}
Alternatively, you can just index into the vector as if it were an array (you don't need to use the .at() method if you don't want to):

Code:
for (int index = 0; index < objects.size(); index++)
{
    if (objects[index] == object) { found = true; }
}

(It's often quite difficult to guess the correct way to dereference an iterator, so it's possible that I mistyped the example above... let me know if it doesn't work.)

Neil Carter
Nether - Mac games and comic art
2004.08.25 06:55 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Josh Offline
Senior Member
****

Posts: 266
Joined: Mar 2005
Post: #3
Vector Troubles
It should also be noted that "vector.h" is deprecated, use "vector" instead. I am also surprised that it does not require you to define your namespace in order to use it. :???:
2004.08.25 08:23 AM
Find all posts by this user Quote this message in a reply
Nick Offline
Senior Member
****

Posts: 271
Joined: Feb 2005
Post: #4
Vector Troubles
When I use vector and not vector.h it tells me I'm using vector as a type but it is not declared.
2004.08.25 12:39 PM
Visit this user's website Find all posts by this user Quote this message in a reply
aaronsullivan Offline
Junior Member
**

Posts: 22
Joined: Apr 2005
Post: #5
Vector Troubles
namespaces. Smile
The vector class and all the STL stuff is in the standard namespace: std
So... you need either a
std::
before each use of vector or better put one of these:
using namespace std;
before any use of vector (or anything else like cout, etc in the standard namespace.) The "using namespace std" means everything after it (in a particular file) uses that namespace... so you only need it once. You can use your own namespaces for giant projects too, but for modest sized projects you won't need any others.
2004.08.25 01:03 PM
Find all posts by this user Quote this message in a reply
Nick Offline
Senior Member
****

Posts: 271
Joined: Feb 2005
Post: #6
Vector Troubles
Ok thanks a ton.
2004.08.25 01:11 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Nick Offline
Senior Member
****

Posts: 271
Joined: Feb 2005
Post: #7
Vector Troubles
NCarter Wrote:
Code:
for (int index = 0; index < objects.size(); index++)
{
    if (objects[index] == object) { found = true; }
}

(It's often quite difficult to guess the correct way to dereference an iterator, so it's possible that I mistyped the example above... let me know if it doesn't work.)

That doesn't work for me. I get an error saying "SDrawList.cpp:22: error: no match for `CObject& == CObject&' operator".

Also look at this code:

Code:
void SDrawList::RemoveObject(CObject &object)
{
    for (int index = 0; index <= objects.size(); index++)
    {
        if (objects[index] == object) { objects.erase(index); }
    }
}

It says I cannot use an int for the parameter of erase. How can I make sure that I'm using an int to do the indexing and switch it to an iterator to erase?
2004.08.25 01:14 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Sohta
Unregistered

 
Post: #8
Vector Troubles
Quote:That doesn't work for me. I get an error saying "SDrawList.cpp:22: error: no match for `CObject& == CObject&' operator".
Did you override the == operator for your CObject class?
2004.08.25 01:55 PM
Quote this message in a reply
Nick Offline
Senior Member
****

Posts: 271
Joined: Feb 2005
Post: #9
Vector Troubles
Yeah I took care of that but I get a ton of warnings from it. I think I'm going to find another way of doing this. I get too many errors and warnings. Once I only had 7 warnings and 0 errors I ran it and nothing showed up. Then it crashed. I'd like to use vectors because they seem easy, but they are giving me much trouble. I'll probably come back to it later.
2004.08.25 02:10 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Codemattic Offline
Member
***

Posts: 147
Joined: Feb 2005
Post: #10
Vector Troubles
I think instead of adding objects to your vector - you really want to add object pointers to your vector.

vector<CObject *> objects;

That way you can add not only CObject objects - but also any CObject subclasses - which I assume - is what you want. But now memory management is much trickier - you have to decide when and how these objects get deleted.
2004.08.25 02:49 PM
Find all posts by this user Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)