error: cannot convert 'std::string' to 'std::string*' in assignment??

This is a discussion on error: cannot convert 'std::string' to 'std::string*' in assignment?? within the C++ Programming forums, part of the General Programming Boards category; This is weird... My compiler just gave me the following message: error: cannot convert 'std::string' to 'std::string*' in assignment in ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question error: cannot convert 'std::string' to 'std::string*' in assignment??

    This is weird...
    My compiler just gave me the following message:
    error: cannot convert 'std::string' to 'std::string*' in assignment
    in regards to this code:
    Code:
    string* CenumOperations::getEnum() {
    
        string* pEnumStrings = enumStrings; //get a pointer to the string member 'enumStrings'
        return pEnumStrings; //return the pointer
    
    }
    The error is at the line where I create a string pointer and initialize it to point at the 'enumStrings' member string of the class "CenumOperations" in the same line.

    And if, I change it to this instead:
    Code:
    string* CenumOperations::getEnum() {
    
        string* pEnumStrings = NULL;
        pEnumStrings = enumStrings; //get a pointer to the string member 'enumStrings'
        return pEnumStrings; //return the pointer
    
    }
    there's no change. Still the same error.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    try string *pEnumStrings = &enumStrings;
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jeffcobb View Post
    try string *pEnumStrings = &enumStrings;
    Oh, duh...I cannot believe I forgot that.
    I guess I got so used to pointing pointers at pointers (or more accurately, pointing pointers at the addresses contained in other pointers) where using the '&' operator is not necessary, that I didn't think to use it for a string instance. Stupid me...

    Thanks.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe returning a reference to a string would be better? I always prefer references over pointers whenever possible.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by cpjust View Post
    Maybe returning a reference to a string would be better? I always prefer references over pointers whenever possible.
    Yeah, I suppose so.
    According to this document, the only advantage that pointers have over references is that they can be incremented to the next location in memory, while references can't. References appear to be safer and also easier to use, because you don't have to use the dereferencing * operator.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Don't feel bad Programmer_ninja; you try more than many how come here for advice. Because you try you will get more help than most too
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Pointers can also be NULL, which makes them useful for optional parameters, they are also better suited for functions exposed through a DLL interface because that makes them useable through other languages.
    But yes, certainly if the value is never meant to be optional or externally exposed, then using a reference is best.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Also, pointers can be reassigned; references cannot. Another important fact between them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Quote Originally Posted by Elysia View Post
    Also, pointers can be reassigned; references cannot. Another important fact between them.
    Sure they can...

    Code:
    
    template < typename Type >
    struct reference
    {
        reference( Type& data )
        : data( data )
        {    }
        
        Type& 
            data;
    };
    
    int main( void )
    {
        int 
            i = 1024,
            j = i + 1;
        reference< int >
            r( i );
        cout << r.data << endl;
        new( &r ) reference< int >( j );
        cout << r.data << endl;
    }
    ...it just ain't kosher.



    ITSA
    Socket Library!

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    I could think of better ways to do that. But regardless, that isn't a reference. It's a class that emulates a reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    I could think of better ways to do that.
    Such as..?

    But regardless, that isn't a reference. It's a class that emulates a reference.
    Or rather, a class that *contains* a reference. At any rate, the code I posted does, in fact, "reassign a reference", so you are still wrong.



    ITSA
    Socket Library!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Quote Originally Posted by Sebastiani View Post
    Such as..?
    I was thinking of a "reassign" member function, instead of having to use placement new.

    Or rather, a class that *contains* a reference. At any rate, the code I posted does, in fact, "reassign a reference", so you are still wrong.
    No, it doesn't. What it does is create a new struct and initializes that reference. So while you could say it reassigns it, technically it does not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Sebastiani View Post
    Or rather, a class that *contains* a reference. At any rate, the code I posted does, in fact, "reassign a reference", so you are still wrong.
    If you start posting non-kosher code examples like that, I think you're obliged to pay attention to the distinction between "reassigning a reference" and "unscroupulously causing the underlying memory occupied by a reference to change".

    No code example like that is going to negate the fact that references cant be reassigned.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    No, it doesn't. What it does is create a new struct and initializes that reference. So while you could say it reassigns it, technically it does not.
    Sorry, but that's incorrect - the code just invokes the constructor on an existing object, so the reference member 'data' does indeed get reassigned.

    Gawd, I love it when you're wrong, Elysia - you'll go to almost any length to avoid admitting it!



    ITSA
    Socket Library!

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    And what does the constructor do? Hint: it constructs a new object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Convert uint16_t to std::string?
    By magicalo in forum C++ Programming
    Replies: 3
    Last Post: 07-18-2008, 01:34 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. What is the easies way to convert int to std::string?
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 12-04-2007, 01:15 AM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21