-1

i'm trying to call a function in another .h file named display that receives a pointer for a std::vector<vector<double> > but when i try to call it i get the error that variable was not declared in this scope. Here's a sample of the code, i hope you can help.

//proper includes above
#include <vector>
#include "display.h"
#include "extract_balls.h"

int main(void)   
{
    std::vector<vector<double> > balls_centroids;
    std::vector<vector<double> > balls_centroids_computed;

    balls_centroids = extract_balls();

    for (vector< vector<double> >::size_type u = 0; u < balls_centroids.size(); u++) {
        vector<double> x = balls_centroids.at(u);
        x[0] = farest_point[0]-x[0];
        x[1] = farest_point[1]-x[1];
        x[2] = farest_point[2]-x[2];
        balls_centroids_computed.push_back(x);
    }

    display(&balls_centroid_computed);
}

The display.h file is something like this:

#include <vector>

void display(std::vector<std::vector<double> >& coord){

           //do stuff
}
Caesar
  • 9,483
  • 8
  • 40
  • 66
ViriatoPT
  • 3
  • 2

3 Answers3

5

You declare:

std::vector<vector<double> > balls_centroids_computed;

but you try to call

display(&balls_centroid_computed);

Note the difference between singular _centroid_ and plural _centroids_...

twalberg
  • 59,951
  • 11
  • 89
  • 84
1

I'm trying to call [...] display() that receives a pointer [...]

According to the prototype, display() takes a reference rather than a pointer.

Consequently, you don't need the ampersand in

    display(balls_centroid_computed);
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Hi NPE thanks for the fast reply, i tried removing the ampersand as you said but i still get the same error. – ViriatoPT Jan 03 '13 at 18:41
  • What should be the proper way to pass the vector balls_centroid_computed so that the display function could use it? Should i use a pointer instead of reference? – ViriatoPT Jan 03 '13 at 18:44
  • @ViriatoPT, Pass it without the ampersand. It passes the variable itself, not a copy, because of being taken by reference. – chris Jan 03 '13 at 18:48
  • NPE thanks alot for your time and reply, my mistake re-reading the code before posting. – ViriatoPT Jan 03 '13 at 18:50
0

The display() function doesn't actually receive a pointer as you state.

If you want it to receive a pointer, declare it like:

void display(std::vector<std::vector<double> > * coord){

       //do stuff

}

The ampersand means you're passing a "reference". Suggest you only change

display (balls_centroid_computed)

Since the function is declared as a reference, you will still get most of the benefits of passing pointers (avoiding putting the whole structure onto the stack).