473,327 Members | 2,094 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,327 software developers and data experts.

error compiling


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 `
....
Aug 10 '05 #1
3 1393
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
kb******@gascad.at
Aug 10 '05 #2
> 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

Aug 10 '05 #3
thank you.
Aug 10 '05 #4

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

Similar topics

0
by: Matt | last post by:
Just out of the blue all of my code has given a 'Could not instantiate the resource processor.' error on compiling. This apparently means your installation of VS .Net has lost the plot. So I've...
6
by: bmwrob | last post by:
I’m very new in C++ programming and am trying to make a decoder tool just for the purpose of learning. I started my project in VC++ 6.0, but after a change of PC, I continued my programming in VS...
2
by: Qiao Yun | last post by:
I used vc++.net (visual studio .net ) to open a project which can work well in vc++6.0. I succeeded in compiling the project in vc++.net in release mode . But when I tried to compile the project...
0
by: Herman Jones | last post by:
I'm getting the following error when I build a Class Library project: Embedding manifest... Project : error PRJ0002 : Error result 1 returned from 'C:\WINDOWS\system32\cmd.exe'. It happens with...
2
by: teddybyte | last post by:
my script below is: #include "stdafx.h" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, ...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
1
by: sahilrekhi | last post by:
Hi i am working on a graphical user interface deisgn programme. when i compile the file i i get the following output with the mentioned errors: 1>------ Rebuild All started: Project: guiq,...
1
by: SQACPP | last post by:
I have an error when compiling a simple form project... I just start a new Form project and add a new PictureBox in a form(or anything that generate the .resx file) After that when compiling I...
0
by: dami.gupta | last post by:
I am building a chm file using sandcastle and am following instructions on https://blogs.msdn.com/sandcastle/archive/2006/07/29/682398.aspx I am following all the commands which are...
6
by: JR | last post by:
The code below compiles and runs perfectly in Windows XP Pro, Using MS VS 2005. If I compile with g++ (cygwin) using the following command line: g++ -pedantic -Weffc++ -Wall -Wctor-dtor-privacy ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.