Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error compiling

Reply
Thread Tools

error compiling

 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-10-2005
Baloff wrote:
>
> 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.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Srini
Guest
Posts: n/a
 
      08-10-2005
> 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

 
Reply With Quote
 
 
 
 
Baloff
Guest
Posts: n/a
 
      08-10-2005

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 `
....
 
Reply With Quote
 
Baloff
Guest
Posts: n/a
 
      08-10-2005
thank you.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 09:47 PM
Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 08:55 PM
Problems with ld: Unresolved error C++ compiling error stevenruiz@gmail.com C++ 2 02-13-2007 09:04 PM
Problem with C++ Compiling Error ld: Unresolved Error stevenruiz@gmail.com C++ 2 02-13-2007 12:01 AM
Compiling when libedit is in path Is there a trick to compiling Ruby when libedit must exist in the search path? Can you statically link to readline 5.0 in some manner? -- Lon Baker Lon Baker Ruby 1 03-21-2005 08:57 AM



Advertisments