Thread: help: function returns typedef const

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    help: function returns typedef const

    I am trying to use a constant typedef returned from a function. I need help in getting a const char * returned from the function. A test case is shown below.

    t1.h:
    ====
    typedef char Char;
    typedef Char * CharPtr;

    const CharPtr getChar();

    t1.c:
    ====
    #include "t1.h"

    #define GREETING

    void main()
    {
    const char * p;
    p = getChar();
    }

    const CharPtr getChar()
    {
    return GREETING;
    }

    Now when I compile this I get " Function cannot return a const qualified type" on both the prototype and the definition.

    Now I know that a function can return a pointer to a constant object but not a constant object.

    And I did find one description in my "C++ Primer" book stating that the const modifies the type of cstr. So that in this case getChar is returning "char * const" and not a "const char *" as expected.

    But I don't know how to modify the typedef in order to get "const char *". Any help appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > #define GREETING

    > return GREETING;

    This is wrong. You cannot simply return 'GREETING' because 'GREETING' has no value. It is simply defined. You have to define it as something to be able to return it. Otherwise, there is nothing to return. This would work:

    #define GREETING 1
    return GREETING;

    #define GREETING "Hello bob."
    return GREETING;

    Both are valid, assuming you return them with the correct function (ie: return an int when the function expects to return an int)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    3
    You are right - GREETING should have a value. I'm sorry my test case did have the value "Hi, how are you" - I missed typing it in on my post.

    With the #define GREETING "Hi, how are you" I still get the same compiler error.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > const char * p;

    Change that to:

    const CharPtr p;

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    3
    That doesn't work either. "const CharPtr p" ends up being "char * const" ie a constant pointer to a char instead of a pointer to a constant char *

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM