473,385 Members | 1,409 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,385 software developers and data experts.

Conversion function problems

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;
}
Jul 22 '05 #1
14 2083

"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com...
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;
}


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
Jul 22 '05 #2
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com...
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

Jul 22 '05 #3
"David White" <no@email.provided> wrote in message
news:cM******************@nasal.pacific.net.au...
class foo_t
{
public:
operator double (*)(int)() {return foo_func;} // Compile error
here!
operator double (*())(int n) { return foo_func;}


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

Jul 22 '05 #4
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com
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;
}


Do you really want to convert objects to function pointers or do you just
want an object that acts like a function? In the latter case, you can do it
with this:

class foo_t
{
public:
double operator()(int n)
{
return n + 1.5;
}
};
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #5

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:bq*************@ID-192448.news.uni-berlin.de...

"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com...
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;
}


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


Thx!!! Just out of curiosity, is it possible to do this without a typedef???
I've tried a bunch of different ideas, but none work. Is it syntactically
possible to do this without a typedef, or is a typedef required? For that
matter, is a typedef ever *required*???
Jul 22 '05 #6

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au...
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com
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;
}
Do you really want to convert objects to function pointers or do you just
want an object that acts like a function? In the latter case, you can do

it with this:

class foo_t
{
public:
double operator()(int n)
{
return n + 1.5;
}
};
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


I am indeed specifically trying to learn how to create a coversion function
to function pointer. My goal is to learn exactly this...
Jul 22 '05 #7

"David White" <no@email.provided> wrote in message
news:cM******************@nasal.pacific.net.au...
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com...
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


That was my next best guess too, but no go (unless it's a problem with my
compiler)!!
Jul 22 '05 #8
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com
"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:bq*************@ID-192448.news.uni-berlin.de...

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


Thx!!! Just out of curiosity, is it possible to do this without a
typedef??? I've tried a bunch of different ideas, but none work. Is
it syntactically possible to do this without a typedef, or is a
typedef required? For that matter, is a typedef ever *required*???


According to Lippman's C++ Primer (3rd ed), p. 777:
"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

This is also discussed less plainly in the C++ standard, section 12.3.2.
Apparently using function pointers without a typedef creates ambiguities in
parsing the expression.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #9

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"Dave" <be***********@yahoo.com> wrote in message
news:vs************@news.supernews.com
"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:bq*************@ID-192448.news.uni-berlin.de...

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
Thx!!! Just out of curiosity, is it possible to do this without a
typedef??? I've tried a bunch of different ideas, but none work. Is
it syntactically possible to do this without a typedef, or is a
typedef required? For that matter, is a typedef ever *required*???


According to Lippman's C++ Primer (3rd ed), p. 777:
"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef

name. Conversion functions in which type represents either an array or a function type are not allowed."

This is also discussed less plainly in the C++ standard, section 12.3.2.
Apparently using function pointers without a typedef creates ambiguities in parsing the expression.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Ahh, OK, cool! Can anybody cite any other cases where a typedef is
*required* to accomplish something???
Jul 22 '05 #10
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
According to Lippman's C++ Primer (3rd ed), p. 777:
"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name. Conversion functions in which type represents either an array or a function type are not allowed."
But you can't return an array or function from any other kind of function
either, not just a conversion operator, with or without a typedef.
This is also discussed less plainly in the C++ standard, section 12.3.2.
Apparently using function pointers without a typedef creates ambiguities in parsing the expression.


Even if the standard doesn't allow it, I'm not convinced that the Lippman
extract has anything to do with requiring a typedef to return a function
pointer. Does he give the general form of a function as this?
type name(/*parameters*/)

which excludes the tiny fraction of functions that don't conform, such as:
double (*f())(int);

(It is just a primer, after all).

DW

Jul 22 '05 #11
"David White" <no@email.provided> wrote in message
news:vY******************@nasal.pacific.net.au
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
According to Lippman's C++ Primer (3rd ed), p. 777:
"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a
typedef name. Conversion functions in which type represents either
an array or a function type are not allowed."


But you can't return an array or function from any other kind of
function either, not just a conversion operator, with or without a
typedef.


The 1998 standard say this in section 8.3.5, p4:

"Functions shall not have a return type of type array or function, although
they may have a return type of type pointer or reference to such things."

The following is an example that works (without even a typedef):

#include <iostream>
using namespace std;

double f0(double n)
{
return n;
}
double f1(double n)
{
return n+1;
}
double f2(double n)
{
return n+2;
}

// this is a function taking an integer argument
// and returning a pointer to a function that
// takes a double as argument and returns a double

double (*function(int x))(double)
{
if(x==1)
return f1;
else if(x==2)
return f2;
else
return f0;
}

int main()
{
cout << function(1)(9.3)<< endl; // expect 10.3
cout << function(2)(9.3)<< endl; // expect 11.3
return 0;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #12
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"David White" <no@email.provided> wrote in message
news:vY******************@nasal.pacific.net.au
But you can't return an array or function from any other kind of
function either, not just a conversion operator, with or without a
typedef.
The 1998 standard say this in section 8.3.5, p4:

"Functions shall not have a return type of type array or function, although
they may have a return type of type pointer or reference to such things."

The following is an example that works (without even a typedef):

#include <iostream>
using namespace std;

double f0(double n)
{
return n;
}
double f1(double n)
{
return n+1;
}
double f2(double n)
{
return n+2;
}

// this is a function taking an integer argument
// and returning a pointer to a function that
// takes a double as argument and returns a double

double (*function(int x))(double)


And in my previous post, this was a function returning a pointer to a function taking an int
parameter and returning a double:
double (*f())(int);
{
if(x==1)
return f1;
else if(x==2)
return f2;
else
return f0;
}

int main()
{
cout << function(1)(9.3)<< endl; // expect 10.3
cout << function(2)(9.3)<< endl; // expect 11.3
return 0;
}


I'm not sure what your point is. The statement you seem to have replied to remains the case,
i.e., that you can't return a function or an array from anything.

DW

Jul 22 '05 #13
"David White" <no@email.provided> wrote in message
news:85******************@nasal.pacific.net.au

I'm not sure what your point is. The statement you seem to have
replied to remains the case, i.e., that you can't return a function
or an array from anything.


Well...I wasn't sure what your point was. I thought (though I wasn't sure)
that you were disputing that there was a special need for typedefs where
conversion operators are concerned. I was taking it as given that we were
interested in the return of pointers to functions rather than the return of
functions.

I now think that you are simply disputing my interpretation of the Lippman
quote:

"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name.
Conversion functions in which type represents either an array or a function
type are not allowed."

You will note that Lippman makes no reference at all to pointers, yet a
conversion operator can certainly return, say, a pointer to a class type.
Accordingly, I interpreted all his statements about types as including
pointers to those types. Thus I interpret him as saying that a conversion
operator cannot return an explicit function pointer but can return a typedef
name for a function pointer.

I admit that this interpretation is arguable, but it does seem to conform to
what I observe of compiler behaviour.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #14
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au...
"David White" <no@email.provided> wrote in message
news:85******************@nasal.pacific.net.au

I'm not sure what your point is. The statement you seem to have
replied to remains the case, i.e., that you can't return a function
or an array from anything.

Well...I wasn't sure what your point was. I thought (though I wasn't sure)
that you were disputing that there was a special need for typedefs where
conversion operators are concerned. I was taking it as given that we were
interested in the return of pointers to functions rather than the return

of functions.

I now think that you are simply disputing my interpretation of the Lippman
quote:
That's right.
"A conversion function takes the general form
operator type();
where type is replaced by a built-in type, a class type, or a typedef name. Conversion functions in which type represents either an array or a function type are not allowed."

You will note that Lippman makes no reference at all to pointers, yet a
conversion operator can certainly return, say, a pointer to a class type.
Accordingly, I interpreted all his statements about types as including
pointers to those types. Thus I interpret him as saying that a conversion
operator cannot return an explicit function pointer but can return a typedef name for a function pointer.


Yes, I'm not as sure about my interpretation now. Even a primer should get
it right, but I was unfairly implying that it might be a little sloppy.

DW

Jul 22 '05 #15

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

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
22
by: kalio80 | last post by:
Hi everyone I am trying to create a file that converts text files from unix to windows and windows to unix I understand the general concept of it as unix uses line feed LF Windows uses CRLF...
5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
1
by: Jimmy Seow | last post by:
Dear All, Hope somebody can help me with this. I have created a class and have got it to serialize and deserialize using XmlSerialization without any problems. I've also read the documentation...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
10
by: Jeroen | last post by:
Hi guys, Just another question. Suppose I have 2 classes (incomplete code): class A { A(const B& b); A& operator = (const A& a); }; class B {
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.