function pointer problem

Hello there, I've study C++ for a while now and encountered this problem when I was doing my exercises.

Basically I have class Data like so
1
2
3
4
5
6
7
8
9
10
class Data
{
  protected:
   int number;
   double *x; //array contain "number" amount of items
  public:
   void setX(int, double);//set the individual x values
   double getX(int);//get the value of x
   double *getX(); //get a pointer to x values
} 

and functions like these
1
2
3
4
5
6
7
8
void Data::setX(int i, double d)
{
    x[i]=d;
}
double Data::getX(int i)
{
    return x[i];
}


and I try to write the last function like this
1
2
3
4
5
6
7
8
9
10
11
12
double Data:: *getX()
{
    return x;
    //it gave me error "x is not declared in this scope"
    //and when I try this
    return &getX;
    //it says otherwise
    //"error: cannot convert 'double Data::* (*)()' to 'double Data::*' in return|"
   //or like this
   return Data::getX;
   //"error: cannot resolve overloaded function 'getX' based on conversion to type 'double Data::*'|"
}

I'm not allowed to change the function's name or modify the class Data

Is there any thing I misunderstood or mistakes I made?

Thank you in advance for your help.
1
2
3
double* Data::getX(){
  return x;
}
1
2
3
4
double* Data::getX()
{
  return x;
}



Thank you very much, that has solve the problem.

What would I need to do to call this function from another file and stored the value in a pointer?

I've created function like this

1
2
3
4
5
6
void Calc::calc(Data &d)
{
   double *_x;
   //and is this correct?
   _x = d.getX;
}

Topic archived. No new replies allowed.