转换功能问题 [英] Conversion function problems
问题描述
大家好,
有人可以帮忙解决下面的问题吗?我正在尝试定义一个转换
函数,它将对象转换为函数指针,并在下面指定的行上得到编译
错误。编译器解释这是一个
函数返回一个函数,当然这是非法的...
正确的语法是什么来实现这个?
谢谢,
戴夫
#include< iostream>
使用命名空间std;
double foo_func(int n){return n + 1.5;}
class foo_t
{
public:
operator double(*)(int)(){return foo_func;} //这里编译错误!
};
int main()
{
foo_t f;
cout<< f(12)<< ENDL; //期待13.5...
返回0;
}
Hello all,
Can anybody help with the problem below? I''m trying to define a conversion
function that converts objects to function pointers and am getting a compile
error on the line indicated below. The compiler interprets this is a
function returning a function, which, of course is illegal... What is the
correct syntax to accomplish this?
Thanks,
Dave
#include <iostream>
using namespace std;
double foo_func(int n) {return n + 1.5;}
class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error here!
};
int main()
{
foo_t f;
cout << f(12) << endl; // Expecting "13.5"...
return 0;
}
推荐答案
" Dave"其中p是*********** @ yahoo.com>在消息中写道
news:vs ************ @ news.supernews.com ...
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com...
大家好,
有人可以帮助解决下面的问题吗?我正在尝试定义一个转换
函数,它将对象转换为函数指针,并在下面指定的行上出现编译错误。编译器解释这是一个返回函数的函数,当然这是非法的......实现这个的正确语法是什么?
谢谢,>戴夫
#include< iostream>
使用命名空间std;
double foo_func(int n){return n + 1.5;}
类foo_t
公共:
operator double(*)(int)(){return foo_func;} //在这里编译错误!
} ;
int main()
{
foo_t f;
cout<< f(12)<< ENDL; //期待13.5......
返回0;
}
Hello all,
Can anybody help with the problem below? I''m trying to define a conversion
function that converts objects to function pointers and am getting a compile
error on the line indicated below. The compiler interprets this is a
function returning a function, which, of course is illegal... What is the
correct syntax to accomplish this?
Thanks,
Dave
#include <iostream>
using namespace std;
double foo_func(int n) {return n + 1.5;}
class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error here!
};
int main()
{
foo_t f;
cout << f(12) << endl; // Expecting "13.5"...
return 0;
}
试试这个 -
#include< iostream>
using namespace std;
double foo_func(int n){return n + 1.5; }
typedef double(* fp)(int);
class foo_t
{
public:
运算符fp(){return foo_func;}
};
int main()
{
foo_t f;
cout<< f(12)<< ENDL; //期待13.5......
返回0;
}
HTH,
J.Schafer
Try this -
#include <iostream>
using namespace std;
double foo_func(int n) {return n + 1.5;}
typedef double (*fp)(int) ;
class foo_t
{
public:
operator fp(){return foo_func;}
};
int main()
{
foo_t f;
cout << f(12) << endl; // Expecting "13.5"...
return 0;
}
HTH,
J.Schafer
" Dave"其中p是*********** @ yahoo.com>在消息中写道
news:vs ************ @ news.supernews.com ...
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com...
大家好,
有人可以帮助解决下面的问题吗?我正在尝试定义一个
转换函数,它将对象转换为函数指针,并在下面指定的行上得到
编译错误。编译器解释这是一个返回函数的函数,当然这是非法的......实现这个的正确语法是什么?
谢谢,>戴夫
#include< iostream>
使用命名空间std;
double foo_func(int n){return n + 1.5;}
类foo_t
公共:
operator double(*)(int)(){return foo_func;} //在这里编译错误!
Hello all,
Can anybody help with the problem below? I''m trying to define a conversion function that converts objects to function pointers and am getting a compile error on the line indicated below. The compiler interprets this is a
function returning a function, which, of course is illegal... What is the
correct syntax to accomplish this?
Thanks,
Dave
#include <iostream>
using namespace std;
double foo_func(int n) {return n + 1.5;}
class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error here!
operator double(*())(int n){return foo_func;}
DW
operator double (*())(int n) { return foo_func;}
DW
" David White" < no@email.provided>在消息中写道
news:cM ****************** @ nasal.pacific.net.au ...
"David White" <no@email.provided> wrote in message
news:cM******************@nasal.pacific.net.au...
class foo_t
{
public:
operator double(*)(int)(){return foo_func;} //编译错误
class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error
here!
operator double(*())(int n){return foo_func;}
here!
operator double (*())(int n) { return foo_func;}
对不起,我的编译器正在玩我的技巧。我确定编译时没有
错误一次,但它现在不会这样做。我不知道为什么。但是,这有效:
double foo_func(int n){return n + 1.5;}
typedef double(* ff)(int n);
class foo_t
{
public:
operator ff(){return foo_func;}
};
DW
Sorry, my compiler is playing tricks on me. I''m sure that compiled without
error once, but it won''t do it now. I don''t know why. However, this works:
double foo_func(int n) {return n + 1.5;}
typedef double (*ff)(int n);
class foo_t
{
public:
operator ff() { return foo_func;}
};
DW
这篇关于转换功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!