From: Jimbo on
Hello

I am trying to store a group of objects in a vector of pointers(of the
same type as the objects) but my simple program is not compiling.

Am I using this vector correcly? vector <object*> objList;

[source]
// Simple example

#include <iostream>
#include <vector>

using namespace std;

class object {

public:
object();
void setCoords(object o);
void getCoords(int &x, int &y);
private:
int x;
int y;
};

vector <object*> objList;

int main() {

// create 4 objects
for (int i=0; i<5; i++) {

object a;
objList.push_back(&a);
}

// change objects x,y values
for (int i=0; i<5; i++) {

*(objList.at(i)).setCoords(i,i); // error: "No matching call
to object::setCoords....
}

int tempX, tempY;

// get & display all objects x,y values
for (int i=0; i<objList.size(); i++)
{
object temp = *(objList.at(i));
temp.getCoords(tempX,tempY); // error: "No matching call to
object::setCoords....
cout << tempX, << " " << tempY << endl; // this will output
1,1 for all of them
// when it should be 1,1 2,2 3,3 4,4
}


}

object::object()
{
x = 1;
y = 1;
}

void object::setCoords(int xN, int yN)
{
x = xN;
y = yN;
}

void object::getCoords(int &xN, int &yN)
{
xN = x;
yN = y;
}

[/source]
From: Alf P. Steinbach on
* Jimbo:
>
> I am trying to store a group of objects in a vector of pointers(of the
> same type as the objects) but my simple program is not compiling.
>
> Am I using this vector correcly? vector <object*> objList;

This is a bit off-topic in [comp.os.ms-windows.programmer.win32].

Consequently I've set follow-ups to [comp.lang.c++].


> [source]
> // Simple example
>
> #include <iostream>
> #include <vector>
>
> using namespace std;
>
> class object {
>
> public:
> object();
> void setCoords(object o);
> void getCoords(int &x, int &y);
> private:
> int x;
> int y;
> };
>
> vector <object*> objList;
>
> int main() {
>
> // create 4 objects
> for (int i=0; i<5; i++) {
>
> object a;
> objList.push_back(&a);
> }

Don't store addresses to local objects. Each local object ceases to exist at the
bottom of the loop body. Leaving you with addresses of non-existent objects.


>
> // change objects x,y values
> for (int i=0; i<5; i++) {
>
> *(objList.at(i)).setCoords(i,i); // error: "No matching call
> to object::setCoords....

This would be ...

objList.at(i)->setCoords(i,i);

.... except that supplying two int arguments doesn't match the declaration of the
member routine.


> }
>
> int tempX, tempY;
>
> // get & display all objects x,y values
> for (int i=0; i<objList.size(); i++)
> {
> object temp = *(objList.at(i));
> temp.getCoords(tempX,tempY); // error: "No matching call to
> object::setCoords....

No, that's not what any compiler may report.



> cout << tempX, << " " << tempY << endl; // this will output
> 1,1 for all of them
> // when it should be 1,1 2,2 3,3 4,4
> }
>
>
> }
>
> object::object()
> {
> x = 1;
> y = 1;
> }
>
> void object::setCoords(int xN, int yN)
> {
> x = xN;
> y = yN;
> }

This does not match the earlier declaration.


> void object::getCoords(int &xN, int &yN)
> {
> xN = x;
> yN = y;
> }
>
> [/source]


Cheers & hth.,

- Alf
From: ScottMcP [MVP] on
On Jan 7, 11:12 pm, Jimbo <nill...(a)yahoo.com> wrote:
>     // create 4 objects
>     for (int i=0; i<5; i++) {
>
>         object a;
>         objList.push_back(&a);
>     }

It creates 4 objects but it also destroys them, because they are
created on the stack.

object* a = new object;
objList.push_back(a);

Creating them this way (on the heap) they will remain in existence.
From: Friedel Jantzen on
Am Fri, 8 Jan 2010 07:02:26 -0800 (PST) schrieb ScottMcP [MVP]:

> On Jan 7, 11:12�pm, Jimbo <nill...(a)yahoo.com> wrote:
>> � � // create 4 objects
>> � � for (int i=0; i<5; i++) {
>>
>> � � � � object a;
>> � � � � objList.push_back(&a);
>> � � }
>
> It creates 4 objects but it also destroys them, because they are
> created on the stack.
>
> object* a = new object;
> objList.push_back(a);
>
> Creating them this way (on the heap) they will remain in existence.

Doesn't it create 5 objects 0,1,2,3,4 ?
BTW: How do you call this error in English?
In German we say "Eins daneben" ~ missed by one.

Friedel
From: ScottMcP [MVP] on
On Jan 9, 2:21 am, Friedel Jantzen <nospam_...(a)freenet.de> wrote:
> Am Fri, 8 Jan 2010 07:02:26 -0800 (PST) schrieb ScottMcP [MVP]:
>
> > On Jan 7, 11:12 pm, Jimbo <nill...(a)yahoo.com> wrote:
> >>     // create 4 objects
> >>     for (int i=0; i<5; i++) {
>
> >>         object a;
> >>         objList.push_back(&a);
> >>     }
>
> > It creates 4 objects but it also destroys them, because they are
> > created on the stack.
>
> > object* a = new object;
> > objList.push_back(a);
>
> > Creating them this way (on the heap) they will remain in existence.
>
> Doesn't it create 5 objects 0,1,2,3,4 ?
> BTW: How do you call this error in English?
> In German we say "Eins daneben" ~ missed by one.
>
> Friedel

Yes, good catch on that error. In English we say "off by one" error.