Passing Array to a Function

Hey Everyone,
I recently ran into a problem when passing 3 arrays into a function that is supposed to move a spacestation sprite around a planet. Any help would be greatly appreciated.

1
2
3
//Background.h
//
void Move(int,float[],float[],float[]);


1
2
3
4
5
6
7
8
9
10
11
12
//Background.cpp
//
void Spacestation::Move(int c, float PlanetSize[], float GlobalPlanetX[], float GlobalPlanetY[])
{
    int theta = 0;

    GlobalStationX[c] = ((PlanetSize[c] + 20) * sin(theta)) + GlobalPlanetX[c];
    GlobalStationY[c] = ((PlanetSize[c] + 20) * cos(theta)) + GlobalPlanetY[c];

    theta++;
    if (theta >= 360) {theta = 0;}
}


1
2
3
4
5
6
//Main.cpp
//
  for(int c = 0; c < 3; c++) {
     stat.CheckDraw(c, global.ViewX, global.ViewY)
     stat.Move(c, planet.PlanetSize, planet.GlobalPlanetX, planet.GlobalPlanetY);
  }


1
2
3
4
5
6
Errors-
error: no matching function for call to 'Spacestation::Move(int&, std::vector<float>&, std::vector<float>&, std::vector<float>&)'

note: candidate is:
note: void Spacestation::Move(int, float*, float*, float*)
note: no known conversion for argument 2 from 'std::vector<float>' to 'float*'


Still fairly new to coding, and I'm not sure where to go from here.
Thanks!
Last edited on
PlanetSize, GlobalPlanetX, and GlobalPlanetY are all pointers to vector, so you have to access their elements with (*x)[n]. Be careful with that because the compiler will perfectly accept x[n] by itself, although it will do something entirely different from what you might otherwise expect.
Would you be able to give me a quick example to go off of? I'm still a bit confused.
Thank you
Bump for help.
You have defined planet.PlanetSize, planet.GlobalPlanetX, and planet.GlobalPlanetY as vector<float> not float* or float[], right?

You need to right your function declaration like this instead:
1
2
3
4
void Spacestation::Move(int c, vector<float> PlanetSize, vector<float> GlobalPlanetX, vector<float> GlobalPlanetY);

//or like this if you want it to run a bit faster (I avoid the details as you said you were new.)
void Spacestation::Move(int c, const vector<float> &PlanetSize, const vector<float> &GlobalPlanetX, const vector<float> &GlobalPlanetY);

Although a vector is like an array, they are not the same thing! (And there is no way to implicitly cast a vector<T> into a T[].)
Last edited on
Thank you so much! That worked like a charm.
Topic archived. No new replies allowed.