Exercise with array of pointers to func
Hi,
I have the following code:
--------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { \
cout << "function " #N " called..." << endl; }
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
void ((*(func_table[]))()) = { &a, &b, &c, &d, &e, &f, &g };
int main() {
while(1) {
cout << "press a key from 'a' to 'g' "
"or q to quit" << endl;
char c, cr;
cin.get(c); cin.get(cr); // second one for CR
if ( c == 'q' )
break; // ... out of while(1)
if ( c < 'a' || c > 'g' )
continue;
(*(func_table[c - 'a']))();
}
}
--------------------------------------------------------------------------------------
and I am supposed to make the functions a(), b(), ... return a string and
print the string in the main(). I initially thought that nothing's more
simple, that I just needd some replacements, like:
--------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
// A macro to define dummy functions:
#define DF(N) string N() { \
return "function " #N " called..."; }
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
string ((*(func_table[]))()) = { &a, &b, &c, &d, &e, &f, &g };
int main() {
while(1) {
cout << "press a key from 'a' to 'g' "
"or q to quit" << endl;
char c, cr;
cin.get(c); cin.get(cr); // second one for CR
if ( c == 'q' )
break; // ... out of while(1)
if ( c < 'a' || c > 'g' )
continue;
cout << ((*(func_table[c - 'a']))()) << endl;
}
}
--------------------------------------------------------------------------------------
But the compiler doesn't agree.
If I substitute
cout << ((*(func_table[c - 'a']))()) << endl;
by
string str=((*(func_table[c - 'a']))());
cout << str << endl;
the first error spoted is still on the cout (I just wanted to make sure the
tricky expression is a real real string even if the compiler says that:
binary '<<' : no operator found which takes a right-hand operand of type
'std::string')
But if I try with a constand string like "hello", it works.
Do you know why?
Do you know how should I do to print the string returned by my functions?
Thank you