error compiling

Discussion in 'C++' started by Karl Heinz Buchegger, Aug 10, 2005.

  1. 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
     
    Karl Heinz Buchegger, Aug 10, 2005
    #1
    1. Advertising

  2. Karl Heinz Buchegger

    Srini Guest

    > 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
     
    Srini, Aug 10, 2005
    #2
    1. Advertising

  3. Karl Heinz Buchegger

    Baloff Guest

    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 `
    ....
     
    Baloff, Aug 10, 2005
    #3
  4. Karl Heinz Buchegger

    Baloff Guest

    thank you.
     
    Baloff, Aug 10, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    411
    Ian Collins
    Feb 13, 2007
  2. Replies:
    2
    Views:
    397
    John Harrison
    Feb 13, 2007
  3. Garrett Cooper
    Replies:
    0
    Views:
    589
    Garrett Cooper
    Feb 24, 2009
  4. Garrett Cooper
    Replies:
    0
    Views:
    522
    Garrett Cooper
    Feb 24, 2009
  5. Lon Baker
    Replies:
    1
    Views:
    236
    Csaba Henk
    Mar 21, 2005
Loading...

Share This Page