What's wrong with this program using template?

P

PengYu.UT

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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};

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

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

Victor Bazarov

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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};

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

int main() {
const interpolator2d<function<int> > inter_f(function<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((function said:
double x = 0;
double y = 0;
inter_f(1.1, 1.1);
}

V
 
K

Kai-Uwe Bux

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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};

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

int main() {
const interpolator2d<function<int> > inter_f(function<int>());

This is parsed as a function declaration. Make that:

const interpolator2d said:
double x = 0;
double y = 0;
inter_f(1.1, 1.1);
}


Best

Kai-Uwe Bux
 
P

PengYu.UT

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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};
template <typename T>
struct function : public std::binary_function<T, T, double> {
double operator()(T x, T y) const {
return - (x * x + y * y);
}
};
int main() {
const interpolator2d<function<int> > inter_f(function<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((function said:
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
 
V

Victor Bazarov

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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};
template <typename T>
struct function : public std::binary_function<T, T, double> {
double operator()(T x, T y) const {
return - (x * x + y * y);
}
};
int main() {
const interpolator2d<function<int> > inter_f(function<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((function said:
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
 
J

John Carson

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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};
template <typename T>
struct function : public std::binary_function<T, T, double> {
double operator()(T x, T y) const {
return - (x * x + y * y);
}
};
int main() {
const interpolator2d<function<int> > inter_f(function<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((function said:
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(function<int>());

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

VJ

Kai-Uwe Bux said:
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(function<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()(double x, double y) const {
return _f(x, y);
}
private:
F _f;
};

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

int main() {
const interpolator2d<function<int> > inter_f(function<int>());


This is parsed as a function declaration. Make that:

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((function<double>()));

or not to use double numbers in the last line
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top