Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ references problem

Name: Anonymous 2010-12-08 10:54

I have this function:
void GenerarPuntos(vector<pair<float, float> > &V, const int &num) {

And I call it this way:
GenerarPuntos(V, V.size());

Why keep I getting these two errors?
main.cpp:139: error: invalid initialization of reference of type ‘std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&’ from expression of type ‘const std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >’

main.cpp:8: error: in passing argument 1 of ‘void GenerarPuntos(std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&, const int&)’

Name: Anonymous 2010-12-08 10:58

Now you have two errors...

Name: Anonymous 2010-12-08 10:58

You see that &? There's your problem. Maybe you should read your errors.

Name: Anonymous 2010-12-08 10:59

>>1
I mean, I have no problems passing variables by reference, but when it comes to vectors I get these errors.
And as far as I know, you pass vectors by reference the same way you do it with variables, using the &.
Am I right?

Name: Anonymous 2010-12-08 11:00

GenerarPuntos(&V, &V.size());

Name: Anonymous 2010-12-08 12:08

GenerierenPunkten()

Name: Anonymous 2010-12-08 12:30

>>1
invalid initialization of reference of type ‘std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >&’ from expression of type ‘const std::vector<std::pair<float, float>, std::allocator<std::pair<float, float> > >’

Name: Anonymous 2010-12-08 12:32

The problem is that your using C++.

C++ really is a dirty shit-stained dog. Don't use it.

Also, you appear to Spanish or something like that. That probably doesn't help.

Name: Anonymous 2010-12-08 12:35

void GenerarPutas

Name: Anonymous 2010-12-08 16:07

I don't know what you're doing but it compiles fine here.
But why on earth are you using a reference as the second argument instead of just passing a const int value? Dereferencing anything is much slower than just pushing an integer to the stack.

HIBT?

Name: Anonymous 2010-12-08 16:47

>>9
Excellent!

Name: Anonymous 2010-12-08 17:43

The answer is in the compiler error, as #7 said.  You're calling with a const vector.

Name: Anonymous 2010-12-08 20:28

>>6
GeneratingPointes()

Don't change these.
Name: Email:
Entire Thread Thread List