PDA

View Full Version : vector with own class



T0bi4s
7th January 2010, 17:15
hi people,

i got some troubles with a vector. i need an array of steps( <- i created my own step-class) for loading a stack, because i don't know how many there will be i thought of using a vector. but i actually can't create one and i don't know why.

here my step class:


class step {

public:
step();
step(int a, int b);

int id_sm;
int id_s;
int id_src;

int type;

int dest_vp_x;
int dest_vp_y;
int dest_vp_z;
int dest_vp_dreh;
int dest_x;
int dest_y;
int dest_z;
int dest_dreh;


};


and i tried this one:


QVector<step> steps;
for(int i = 0; i < step_count; i++){

steps.append(new step(42,i));
}

but he says no matching function for this call?

anyone knows why or knows an idea what else i could use?

thx
tobi

faldzip
7th January 2010, 22:47
and i tried this one:


QVector<step> steps;


here you say that vector stores step objects



for(int i = 0; i < step_count; i++){

steps.append(new step(42,i));
}


and here you want to append pointer to an step object so it won't work for sure, it is C++ not Java :P

jefftee
9th January 2010, 06:59
QVector<step> steps;
for(int i = 0; i < step_count; i++) {
steps.append(new step(42,i));
}


Try storing pointers in the vector like this:


QVector<step*> steps;
for(int i = 0; i < step_count; i++) {
steps.append(new step(42,i));
}


I always create a typedef for the templated classes in my code. If you like, you can create a typedef for your step vector like so:



typedef QVector<step*> StepPtrVector;
StepPtrVector steps;

Hope that helps.

jthomps

wysota
9th January 2010, 08:53
But why store pointers if the step class is perfectly value-based? Unless of course you like your programs to leak memory or you like to have more work than required...

jefftee
9th January 2010, 10:15
But why store pointers if the step class is perfectly value-based? Unless of course you like your programs to leak memory or you like to have more work than required...
Check his original post, he's doing a "new step", which is why QVector<step> didn't work...

I was simply pointing out that he was doing a "new step" in line 3, which requires that the QVector template be QVector<step*>.He can store step values, references, or pointers, but the QVector declaration needs to match.

Not sure what you meant by pointers leaking memory. Pointers don't leak memory, bad programmers who don't know how to clean up pointers do...

jthomps

wysota
9th January 2010, 12:09
Check his original post, he's doing a "new step", which is why QVector<step> didn't work...

I was simply pointing out that he was doing a "new step" in line 3, which requires that the QVector template be QVector<step*>.He can store step values, references, or pointers, but the QVector declaration needs to match.
So he should not be doing "new step" but step(). Don't encourage him to do wrong things.


Not sure what you meant by pointers leaking memory. Pointers don't leak memory, bad programmers who don't know how to clean up pointers do...
I mean you have to clear the vector manually after you're done with it (hence "more work") which is not required when you use value-based vectors.

faldzip
9th January 2010, 16:38
He can store step references ...

Can he? Like this:


QList<step &> steps;

???

jefftee
9th January 2010, 23:45
So he should not be doing "new step" but step(). Don't encourage him to do wrong things.

That sums it up right there. You consider using pointers bad and the rest of the C++ world doesn't... :)


Can he? Like this:


QList<step &> steps;


Oops, my bad, scratch the by reference, which leaves by value or by pointer... :)

Regards,

jthomps

wysota
10th January 2010, 15:38
That sums it up right there. You consider using pointers bad and the rest of the C++ world doesn't... :)

No, I don't. But you should know when to use pointers and when not to use them. Which of the following would you prefer and why?


void func1() {
int x;
doSomething(x);
}
or

void func2() {
int *x = new int;
doSomething(*x);
delete x;
}

squidge
10th January 2010, 21:45
Normally I use pointers with vectors simply so that if I need to pass an item somewhere I can simply pass a pointer instead so avoid the object being copied for the parameter. However, starting with Qt, I find it easier to use implicitly shared classes, so the data is shared between the object unless it is modified (copy on write strategy).

jefftee
11th January 2010, 03:59
Of course I'd prefer the first version of the function... :) There's no benefit to passing around pointers to built in types, but if that were a QByteArray or one of my own classes that is fairly large instead of a int, I'd probably use a new/delete and manage the pointers.

BTW, I just noticed that you're the owner (or admin) for Qt Centre site. Great site and love the recent upgrade. Keep up the good work!

Regards,

jthomps

wysota
11th January 2010, 08:36
There's no benefit to passing around pointers to built in types, but if that were a QByteArray or one of my own classes that is fairly large instead of a int,
What's the benefit of passing QByteArray using a pointer compared to an int?

T0bi4s
14th January 2010, 22:06
hi,

it's me again i still got trouble with this vector, and i don't really get your point with what you mean with value-based?
i mean yes the class step is value based, so shouldn't i use pointers? or what you want to tell me?

my c and c++ is quite bad cause i just used java the last 2 years so i may need some generell information about, saving, in this case, steps. :/

wysota
14th January 2010, 23:06
it's me again i still got trouble with this vector, and i don't really get your point with what you mean with value-based?

I mean it contains only POD values and no pointers, so you can safely copy it around.