error compiling

  • Thread starter Karl Heinz Buchegger
  • Start date
K

Karl Heinz Buchegger

Baloff said:
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

Well. Expand the macros by hand to see what you want from the compiler.
// 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);

so DF(a) expands to

string a()
{
return a;
}

So what does 'a' stand for. Well. a denotes a function which takes
no arguments and returns a string. As usual, when the name of a function
does not appear in an actual function call, it decays to a pointer to
that function. Thus

return a;

tries to return a pointer to function a.
But the declaration of that function says that the function returns a string.

main.cpp:9: error: conversion from `std::string (*)()' to non-scalar type `
std::string' requested

std::string (*)()
is C++ speak for: the data type is function which takes no arguments and returns
a std::string

What you probably wanted to do is to have the macro create a function

string a()
{
return "a";
}

you can do it this way:

#define DF(N) string N() { return #N; }

# applied to macro parameters is known as the 'stringize operation'. It turns
the macro parameter into a string.
 
S

Srini

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
 
B

Baloff

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

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 `
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top