473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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(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);
}

Oct 28 '06 #1
6 1431
Pe*******@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(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<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
Oct 28 '06 #2
Pe*******@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(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<function<int inter_f((function<int>()));
double x = 0;
double y = 0;
inter_f(1.1, 1.1);
}

Best

Kai-Uwe Bux
Oct 28 '06 #3


On Oct 28, 6:05 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(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<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

Oct 28 '06 #4
Pe*******@gmail.com wrote:
On Oct 28, 6:05 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(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<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
Oct 29 '06 #5
<Pe*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com
On Oct 28, 6:05 pm, "Victor Bazarov" <v.Abaza...@comAcast.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(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<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(function<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
Oct 29 '06 #6
VJ
Kai-Uwe Bux wrote:
Pe*******@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(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<function<int inter_f((function<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((function<double>()));

or not to use double numbers in the last line
Oct 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
2
by: marco | last post by:
the problem: I use a typedef inside a class template, than I use this type (dim_v<N1>::Type) to define the argument of a template function f but when I call this function from main, the compiler...
4
by: PengYu.UT | last post by:
Hi, It seems that the line with "//no error" and "//error" are the same. However, the second one give an error. I'm using g++-3.3. The error message was: g++-3.3 -g -c -o main.o main.cc...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
6
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
10
by: Protoman | last post by:
Could you tell me what's wrong with this program, it doesn't compile: #include <iostream> #include <cstdlib> using namespace std; class Everything { public: static Everything* Instance()
7
by: mosfet | last post by:
HI, when trying to compile an embedded version of STL called ustl on win32 platform I get the following error : /// Returns the minimum of \p a and \p b template <typename T1, typename T2>...
68
by: DaveJ | last post by:
Recently I was working on a project where I came across an issue where the program cored and reported "free(): invalid pointer". I found a resolution for this, but don't fully understand why the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.