> Hello
this code is putting our errors which are diffecult for me to fix, I
have been thinking about it for a while now...
thanks for helping
******************************code**************** **************
#include <iostream>
#include <string>
using namespace std;
// A macro to define dummy functions:
#define DF(N) string N() { return N; }
DF(a); DF(b); DF(c); DF(d); DF(e); (f); DF(g);
string (*func_table[])() = { a, b, c, d, e, f, g };
The return statement is the culprit. I'm not sure what you're trying to
do. But in case of
DF(a);
Its expanded into...
string a() { return a; }
What is 'a' here? Its a function - so what you're returning is a
pointer to a function that takes nothing and returns a string. Whereas
the function itself is returning a string.
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 << "function " << (*func_table[c - 'a'])()
<< " called" << endl;
}
}
******************************error*************** ***************
make -k
g++ -c -o main.o main.cpp
main.cpp: In function `std::string a()':
main.cpp:9: error: conversion from `std::string (*)()' to non-scalar type `
std::string' requested
main.cpp: In function `std::string b()':
main.cpp:9: error: conversion from `std::string (*)()' to non-scalar type `
...
That's exactly what the error message is saying. It cannot convert
"pointer to function that takes nothing and returns a string" to a
string. And since you've not told what it is that you're trying to
accomplish in your code, I cannot give a solution to this. All I can
say is that the return statement must return a string object.
Srini