What's wrong with this program using template?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PengYu.UT@gmail.com

    #1

    What's wrong with this program using template?

    Hi,

    I couldn't see what is wrong with the following program. Would you
    please help me?

    Thanks,
    Peng


    g++-3.4 -MM -g main.cc .dep
    g++-3.4 -g -c -o main.o main.cc
    main.cc: In function `int main()':
    main.cc:26: error: cannot convert `double' to `function<int(* )()' for
    argument `1' to `const interpolator2d< function<int
    inter_f(functio n<int(*)())'
    make: *** [main.o] Error 1


    #include <functional>

    template <typename F>
    class interpolator2d {
    public:
    interpolator2d( const F &f) : _f(f) {
    }
    typename F::result_type operator()(doub le x, double y) const {
    return _f(x, y);
    }
    private:
    F _f;
    };

    template <typename T>
    struct function : public std::binary_fun ction<T, T, double{
    double operator()(T x, T y) const {
    return - (x * x + y * y);
    }
    };

    int main() {
    const interpolator2d< function<int inter_f(functio n<int>());
    double x = 0;
    double y = 0;
    inter_f(1.1, 1.1);
    }

  • Victor Bazarov

    #2
    Re: What's wrong with this program using template?

    PengYu.UT@gmail .com wrote:
    Hi,
    >
    I couldn't see what is wrong with the following program. Would you
    please help me?
    >
    Thanks,
    Peng
    >
    >
    g++-3.4 -MM -g main.cc .dep
    g++-3.4 -g -c -o main.o main.cc
    main.cc: In function `int main()':
    main.cc:26: error: cannot convert `double' to `function<int(* )()'
    for argument `1' to `const interpolator2d< function<int
    inter_f(functio n<int(*)())'
    make: *** [main.o] Error 1
    >
    >
    #include <functional>
    >
    template <typename F>
    class interpolator2d {
    public:
    interpolator2d( const F &f) : _f(f) {
    }
    typename F::result_type operator()(doub le x, double y) const {
    return _f(x, y);
    }
    private:
    F _f;
    };
    >
    template <typename T>
    struct function : public std::binary_fun ction<T, T, double{
    double operator()(T x, T y) const {
    return - (x * x + y * y);
    }
    };
    >
    int main() {
    const interpolator2d< function<int inter_f(functio n<int>());
    I just wonder how many times people will be posting questions after
    walking into this?..

    Repeat after me: the statement above is a declaration of a FUNCTION,
    not an object as you intended. This is the same as if you wrote

    int a(double());

    intending to construct an integer from a default-initialised double.
    It's a FUNCTION declaration.

    To fix it surround each argument (the only argument in your case)
    with an extra set of parentheses. Should be

    ... inter_f((functi on<int>()));
    double x = 0;
    double y = 0;
    inter_f(1.1, 1.1);
    }
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Kai-Uwe Bux

      #3
      Re: What's wrong with this program using template?

      PengYu.UT@gmail .com wrote:
      Hi,
      >
      I couldn't see what is wrong with the following program. Would you
      please help me?
      >
      Thanks,
      Peng
      >
      >
      g++-3.4 -MM -g main.cc .dep
      g++-3.4 -g -c -o main.o main.cc
      main.cc: In function `int main()':
      main.cc:26: error: cannot convert `double' to `function<int(* )()' for
      argument `1' to `const interpolator2d< function<int
      inter_f(functio n<int(*)())'
      make: *** [main.o] Error 1
      >
      >
      #include <functional>
      >
      template <typename F>
      class interpolator2d {
      public:
      interpolator2d( const F &f) : _f(f) {
      }
      typename F::result_type operator()(doub le x, double y) const {
      return _f(x, y);
      }
      private:
      F _f;
      };
      >
      template <typename T>
      struct function : public std::binary_fun ction<T, T, double{
      double operator()(T x, T y) const {
      return - (x * x + y * y);
      }
      };
      >
      int main() {
      const interpolator2d< function<int inter_f(functio n<int>());
      This is parsed as a function declaration. Make that:

      const interpolator2d< function<int inter_f((functi on<int>()));
      double x = 0;
      double y = 0;
      inter_f(1.1, 1.1);
      }

      Best

      Kai-Uwe Bux

      Comment

      • PengYu.UT@gmail.com

        #4
        Re: What's wrong with this program using template?



        On Oct 28, 6:05 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        PengYu...@gmail .com wrote:
        Hi,
        >
        I couldn't see what is wrong with the following program. Would you
        please help me?
        >
        Thanks,
        Peng
        >
        g++-3.4 -MM -g main.cc .dep
        g++-3.4 -g -c -o main.o main.cc
        main.cc: In function `int main()':
        main.cc:26: error: cannot convert `double' to `function<int(* )()'
        for argument `1' to `const interpolator2d< function<int
        inter_f(functio n<int(*)())'
        make: *** [main.o] Error 1
        >
        #include <functional>
        >
        template <typename F>
        class interpolator2d {
        public:
        interpolator2d( const F &f) : _f(f) {
        }
        typename F::result_type operator()(doub le x, double y) const {
        return _f(x, y);
        }
        private:
        F _f;
        };
        >
        template <typename T>
        struct function : public std::binary_fun ction<T, T, double{
        double operator()(T x, T y) const {
        return - (x * x + y * y);
        }
        };
        >
        int main() {
        const interpolator2d< function<int inter_f(functio n<int>());I just wonder how many times people will be posting questions after
        walking into this?..
        >
        Repeat after me: the statement above is a declaration of a FUNCTION,
        not an object as you intended. This is the same as if you wrote
        >
        int a(double());
        >
        intending to construct an integer from a default-initialised double.
        It's a FUNCTION declaration.
        >
        To fix it surround each argument (the only argument in your case)
        with an extra set of parentheses. Should be
        >
        ... inter_f((functi on<int>()));
        >
        double x = 0;
        double y = 0;
        inter_f(1.1, 1.1);
        }V
        main.cc:26: error: cannot convert `double' to `function<int(* )()' for
        argument `1' to `const interpolator2d< function<int

        I still don't understand the error message. Where is the `double'? Does
        `function<int(* )()' mean a function? Can I declare function in
        arguments of a class constructor?

        Thanks,
        Peng

        Comment

        • Victor Bazarov

          #5
          Re: What's wrong with this program using template?

          PengYu.UT@gmail .com wrote:
          On Oct 28, 6:05 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          >PengYu...@gmai l.com wrote:
          >>Hi,
          >>
          >>I couldn't see what is wrong with the following program. Would you
          >>please help me?
          >>
          >>Thanks,
          >>Peng
          >>
          >>g++-3.4 -MM -g main.cc .dep
          >>g++-3.4 -g -c -o main.o main.cc
          >>main.cc: In function `int main()':
          >>main.cc:26: error: cannot convert `double' to `function<int(* )()'
          >>for argument `1' to `const interpolator2d< function<int
          >>inter_f(funct ion<int(*)())'
          >>make: *** [main.o] Error 1
          >>
          >>#include <functional>
          >>
          >>template <typename F>
          >>class interpolator2d {
          >> public:
          >> interpolator2d( const F &f) : _f(f) {
          >> }
          >> typename F::result_type operator()(doub le x, double y) const {
          >> return _f(x, y);
          >> }
          >> private:
          >> F _f;
          >>};
          >>
          >>template <typename T>
          >>struct function : public std::binary_fun ction<T, T, double{
          >> double operator()(T x, T y) const {
          >> return - (x * x + y * y);
          >> }
          >>};
          >>
          >>int main() {
          >> const interpolator2d< function<int inter_f(functio n<int>());I
          >>just wonder how many times people will be posting questions after
          >>walking into this?..
          >>
          >Repeat after me: the statement above is a declaration of a FUNCTION,
          >not an object as you intended. This is the same as if you wrote
          >>
          > int a(double());
          >>
          >intending to construct an integer from a default-initialised double.
          >It's a FUNCTION declaration.
          >>
          >To fix it surround each argument (the only argument in your case)
          >with an extra set of parentheses. Should be
          >>
          > ... inter_f((functi on<int>()));
          >>
          >> double x = 0;
          >> double y = 0;
          >> inter_f(1.1, 1.1);
          >>}V
          >
          main.cc:26: error: cannot convert `double' to `function<int(* )()'
          for argument `1' to `const interpolator2d< function<int
          >
          I still don't understand the error message. Where is the `double'?
          1.1 is the double.
          Does `function<int(* )()' mean a function?
          Your 'inter_f' *function* takes one argument, a pointer to a function.
          Can I declare function in
          arguments of a class constructor?
          Not sure what you're asking here, sorry. Please rephrase.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • John Carson

            #6
            Re: What's wrong with this program using template?

            <PengYu.UT@gmai l.comwrote in message
            news:1162080786 .785018.105720@ i42g2000cwa.goo glegroups.com
            On Oct 28, 6:05 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            >PengYu...@gmai l.com wrote:
            >>Hi,
            >>
            >>I couldn't see what is wrong with the following program. Would you
            >>please help me?
            >>
            >>Thanks,
            >>Peng
            >>
            >>g++-3.4 -MM -g main.cc .dep
            >>g++-3.4 -g -c -o main.o main.cc
            >>main.cc: In function `int main()':
            >>main.cc:26: error: cannot convert `double' to `function<int(* )()'
            >>for argument `1' to `const interpolator2d< function<int
            >>inter_f(funct ion<int(*)())'
            >>make: *** [main.o] Error 1
            >>
            >>#include <functional>
            >>
            >>template <typename F>
            >>class interpolator2d {
            >> public:
            >> interpolator2d( const F &f) : _f(f) {
            >> }
            >> typename F::result_type operator()(doub le x, double y) const {
            >> return _f(x, y);
            >> }
            >> private:
            >> F _f;
            >>};
            >>
            >>template <typename T>
            >>struct function : public std::binary_fun ction<T, T, double{
            >> double operator()(T x, T y) const {
            >> return - (x * x + y * y);
            >> }
            >>};
            >>
            >>int main() {
            >> const interpolator2d< function<int inter_f(functio n<int>());I
            >>just wonder how many times people will be posting questions after
            >>walking into this?..
            >>
            >Repeat after me: the statement above is a declaration of a FUNCTION,
            >not an object as you intended. This is the same as if you wrote
            >>
            > int a(double());
            >>
            >intending to construct an integer from a default-initialised double.
            >It's a FUNCTION declaration.
            >>
            >To fix it surround each argument (the only argument in your case)
            >with an extra set of parentheses. Should be
            >>
            > ... inter_f((functi on<int>()));
            >>
            >> double x = 0;
            >> double y = 0;
            >> inter_f(1.1, 1.1);
            >>}V
            >
            main.cc:26: error: cannot convert `double' to `function<int(* )()'
            for argument `1' to `const interpolator2d< function<int
            >
            I still don't understand the error message. Where is the `double'?
            Does `function<int(* )()' mean a function? Can I declare function in
            arguments of a class constructor?
            The point is that the compiler is not interpreting your code as a call to
            the class constructor at all.

            const interpolator2d< function<int inter_f(functio n<int>());

            is interpreted as the declaration of a function called inter_f which takes
            an argument of type function<intand returns an object of type const
            interpolator2d< function<int.


            --
            John Carson


            Comment

            • VJ

              #7
              Re: What's wrong with this program using template?

              Kai-Uwe Bux wrote:
              PengYu.UT@gmail .com wrote:
              >
              >
              >>Hi,
              >>
              >>I couldn't see what is wrong with the following program. Would you
              >>please help me?
              >>
              >>Thanks,
              >>Peng
              >>
              >>
              >>g++-3.4 -MM -g main.cc .dep
              >>g++-3.4 -g -c -o main.o main.cc
              >>main.cc: In function `int main()':
              >>main.cc:26: error: cannot convert `double' to `function<int(* )()' for
              >>argument `1' to `const interpolator2d< function<int
              >>inter_f(funct ion<int(*)())'
              >>make: *** [main.o] Error 1
              >>
              >>
              >>#include <functional>
              >>
              >>template <typename F>
              >>class interpolator2d {
              > public:
              > interpolator2d( const F &f) : _f(f) {
              > }
              > typename F::result_type operator()(doub le x, double y) const {
              > return _f(x, y);
              > }
              > private:
              > F _f;
              >>};
              >>
              >>template <typename T>
              >>struct function : public std::binary_fun ction<T, T, double{
              > double operator()(T x, T y) const {
              > return - (x * x + y * y);
              > }
              >>};
              >>
              >>int main() {
              > const interpolator2d< function<int inter_f(functio n<int>());
              >
              >
              This is parsed as a function declaration. Make that:
              >
              const interpolator2d< function<int inter_f((functi on<int>()));
              >
              >
              > double x = 0;
              > double y = 0;
              > inter_f(1.1, 1.1);
              >>}
              >
              >
              >
              Best
              >
              Kai-Uwe Bux
              still does not work. that line needs to be:

              const interpolator2d< function<double inter_f((functi on<double>()));

              or not to use double numbers in the last line

              Comment

              Working...