-4

I am trying to use a class member function in another member function from the same class. Only thing is both class member functions(Class Foo) use arguments which are some parameters which come from some other class(Class Params) which has only data members. I am passing these parameters as constant pointer to both the member functions. When I trying compiling it with all the necessary files I get an error as 'expected expression'. Since the actual code is long and in separate files, I'm just showing how the class implementation file looks like. Any help will be appreciated!

Params::Params(){      // all data members are declared in the   
   double param1;      // constructor of Class Params.
   double param2;
   double param3;
   ...
   ...
   double paramN;      // n-th parameter
}



double Foo::Foo1(const Params & p){
// function body
}

double Foo::Foo2(const Params & p){
   result = p.paramN * Foo1(const Params & p);  // this line is problematic, it is 
                                                // showing the above error in the 
                                                // argument of function Foo1. 
   return result;
}
lakshya91
  • 35
  • 6

1 Answers1

1

you only need to link the variable when passing it through a function, so it should be : result = p.paramN * Foo1(p); instead of : result = p.paramN * Foo1(const Params & p);

Olivier Samson
  • 609
  • 4
  • 13
  • Thanks for your help, it worked! But may I ask why did I just have to link only the variable not the complete `(const Params & p)`? – lakshya91 Jun 18 '18 at 03:29
  • @lakshya91 this is how it works in c++. Basically, you already create the variable 'p' to be used in Foo2's scope in its declaration : `double Foo::Foo2(const Params & p)`. Furthermore (this might be more related to what you thought), the syntax used to define a function's parameters in its declaration only serves to point out what variables are accepted as parameters. So `foo(int a)` would only be able to be called with a variable of type `int` and, when calling it, you only have to put the variable in there in a form which is accepted by the function (defined in its declaration). – Olivier Samson Jun 18 '18 at 03:53
  • @lakshya91 In your case, you already specify that your function `Foo1` takes `const Params &` as a parameter (which is a constant reference to a variable of type `Params` to be specific). Since the variable 'p' in `Foo2` is already like that, you can only pass it through with the variable's name (there's no need to cast it - not like you had the good syntax to cast it anyway). See : http://www.cplusplus.com/doc/tutorial/functions/ – Olivier Samson Jun 18 '18 at 03:57