1

I am trying to make a function of a function using pointers to functions, similar to what is given at the bottom (last section) of this link on www.cplusplus.com, except a little more advanced. I am trying the following:

In myFile.h

// namespace for: Functions
namespace Functions {

    // namespace for: 1D functions
    namespace OneDimensional {

        // Function for: f(x) = x * x, Note: read t as times
        double xtx(double x);
    }

    // namespace for: 2D functions
    namespace TwoDimensional {

    // Function for: f(x, g(y)) = x + g(y), Note: read _ as "of"
    double f_xANDg_y(double x, double(*g)(double y));
    }
}

In myFile.cpp

double Functions::OneDimensional::xtx(double x) {
    return (x * x);
}

double Functions::TwoDimensional::f_xANDg_y(double x, double(*g)(double y)) {
    return (x + (*g)(y));  // <== This is where I get the Error (E0020)
}

I checked the error E0020 and this brought me to Stack Overflow and that user was missing a brace. I checked and I have no missing brace (but I could be wrong even after checking a few times).

Is it that I am implementing this idea of f(x, g(y)) incorrectly, or am I actually missing a brace?

5
  • 2
    What's y? Which value do you want to pass to g? Did you mean to pass y as an extra parameter? It's difficult to tell what you are trying to achieve, but perhaps something like double f_xANDg_y(double x, double y, double(*g)(double)) { return x + g(y); } I don't see the point of the exercise though. Commented Aug 4, 2018 at 23:47
  • @IgorTandetnik My idea is to pass g which is a pointer to some function (any function) which itself has a input parameter y. That is what I am trying to implement in myFile.cpp Commented Aug 4, 2018 at 23:50
  • Well, you did pass the function. Now you also need to pass a value this function should be called with. Commented Aug 4, 2018 at 23:50
  • @IgorTandetnik Ok, I will try with the way you said of trying with 3 input parameters to f_xAND_y, thanks for the help Commented Aug 4, 2018 at 23:52
  • @IgorTandetnik That took care of the error, if you want to post it as an answer you can, either way thanks also the point of this exercise is that I am need these functions for testing numerical methods that I am writing as well Commented Aug 4, 2018 at 23:54

1 Answer 1

3

The error appears here because the compile doesn't know what is "y", since it is not declared any where in the visible scope of the function call. 'f_xANDg_y(double x, double(*g)(double y);' The above function parameter takes only two parameters,

  1. "x" - which 'is of type double'
  2. "g" as pointer to function of type 'double(*)(double)'. which is 'double (*g) (double y)' here 'y' is part of function pointer, your code would have written function perfectly with out using the y.
  3. Here you are trying call your function with a 'y' as input parameter and compiler is not able to find 'double y' in its scope, hence, it throws an error.
  4. Since the whole 'double(*g)(double y)' argument in your function TwoDimenesional boil downs to "a function pointer which take double as input and returns double"

        double Functions::TwoDimensional::f_xANDg_y(double x, double(*g)(double y)) {
       return (x + (*g)(y));  // <== This is where I get the Error (E0020)
    }
    
  5. Your code works fine, you will need to add one more "double" as an argument to your function as shown here TwoDimensional::f_xANDg_y(double x, double(*g)(double), double y). As expected by the compiler.

  6. Your code after making the corrections:

'

namespace Functions {

    // namespace for: 1D functions
    namespace OneDimensional {

        // Function for: f(x) = x * x, Note: read t as times
        double xtx(double x);
    }

    // namespace for: 2D functions
    namespace TwoDimensional {

    // Function for: f(x, g(y)) = x + g(y),a Note: read _ as "of"
    double f_xANDg_y(double x, double(*g)(double), double yParam);
    }
}

double Functions::OneDimensional::xtx(double x) {
    return (x * x);
}

double Functions::TwoDimensional::f_xANDg_y(double x, double(*g)(double), double y) {
    return (x + g(y));
}

'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.