simple but not working

This is a discussion on simple but not working within the C++ Programming forums, part of the General Programming Boards category; Why would the following give an error as shown? Code: std::string messageOut = "header 1: " + "test"; error: invalid ...

  1. #1
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Angry simple but not working

    Why would the following give an error as shown?

    Code:
    std::string messageOut = "header 1: " + "test";
    
    error: invalid operands of types `const char[11]' and `const char[5]' to binary `operator+'
    Until I added the ' + "test" ' everything worked fine.

    Thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Just a wild guess....

    Check the string operators:

    http://www.cppreference.com/cppstrin...operators.html

    See any that for operator+ that takes in two const char *'s?

  3. #3
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    The examples for C++ (unlike those for c stings) hide the use of pointers which lead me to above usage (even at the link given).

    But clearly the link you gave shows that the arguments (even if hidden) are pointers.

    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Of course, there's no reason to do that when you can just do:
    Code:
    std::string messageOut = "header 1: test";
    In the rare case that you want to add two string literals, you can just create a temporary string out of one of them so that you are always adding to a string:
    Code:
    std::string messageOut = "header 1: " + std::string("test");

  5. #5
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    On a related note:

    I am converting a number to a string and would like to code...
    Code:
    std::string DecToBin(int num);
    ...
    messageOut = "header 1:  " + DecToBin(num);
    And for the last hour I've been discovering that whenever and however I invoke DecToBin(num)...
    e.g.
    Code:
    test1 = DecToBin(num);
    messageOut = "header1:  " + test1;
    ...that the executable returns "segmentation fault". I step through the code and find that a normally solid function (elsewhere and upstream in the method) returns negative.

    What I'm wondering is that because I've defined DecToBin(int num) as an outside function to the class method that calls it, is it possible that some of its internal char* methods are conflicting with the class methods memory allocations?

    Mystery...
    Last edited by Dave++; 06-19-2007 at 07:58 PM. Reason: clarification?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    There shouldn't be a conflict if you are doing it right.

    Is this related to your itoa thread? Why are you using char*'s at all?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Dave++:

    If you're returning a local string object, that could be why you're seg faulting. I don't think you should return local objects. Someone correct me if I'm wrong....

    Quote Originally Posted by Daved View Post
    Why are you using char*'s at all?
    ...

    Because string literals are char *'s when you get down to it. That's how you create string objects.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> I don't think you should return local objects.
    It's fine if it is a string object, which is what that function returns.

    >> Because string literals are char *'s when you get down to it.
    I'm not referring to string literals. I'm referring to actual usage and manipulation of the string itself. It sounds as if Dave++ is using char*'s as variables, when all that does is lead to greater potential for errors like segmentation faults. The C++ string class should be used instead.

    The only char*'s you might want are const char*'s to point at the string literals, but even those are probably not necessary.

  9. #9
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    MacGyver,
    Can you expand on your previous comment?

    More testing...
    In gdb I can comment out the call to the function, with a simple default value in its place, and then reset the DAQ hardware and everything works fine, but then as soon as I uncomment the line...everything fails. AND then when I comment the line out again errors persist until I reset the hardware.
    Interestingly, the function itself works alright (set var=newval, and step though an if) but its inclusion alone seems to cause the code to fault ahead of itself and then behind as well.

    So, if I get you right, I need to create a global pointer for the string?

    Dave
    Last edited by Dave++; 06-19-2007 at 09:27 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Can you post the code inside the function?

    >> So, if I get you right, I need to create a global pointer for the string?
    No. If you are returning a string, you don't have to worry about that (Macgyver was mistaken). But it really depends on the code, its hard to tell just by your description.

  11. #11
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    It's the code from Mike

    itoa in C++

    I modified to return std::string
    Code:
    std::string strOut = std::string(str);    // using Daved approach as shown above
    return(strOut);
    fyi - clocking out for the day
    Last edited by Dave++; 06-19-2007 at 09:32 PM. Reason: updates

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The problem is likely that you didn't allocate space for the char* when you moved it inside the function. You could declare str as char str[100] if you are using mike_g's version, but I would use a bitset as I mentioned in that thread:
    Code:
    std::string getbinary(int num)
    {
        return std::bitset<32>(num).to_string<char, std::char_traits<char>, std::allocator<char> >();
    }
    The ugliness of the to_string method will be fixed in later C++ standard.

    For fun, I welcome people to comment on this "super" version, which is probably more than you need:
    Code:
    #include <iostream>
    #include <bitset>
    #include <string>
    #include <climits>
    #include <limits>
    
    template<bool truncate_zeroes, size_t bits>
    std::string get_binary_impl(unsigned long val)
    {
        std::string s(std::bitset<bits>(val).to_string<char, std::char_traits<char>, std::allocator<char> >());
        return truncate_zeroes ? (val == 0 ? "0" : s.substr(s.find_first_not_of('0'))) : s;
    }
    
    template<size_t bits>
    std::string get_binary(unsigned long val)
    {
        return get_binary_impl<false, bits>(val);
    }
    
    std::string get_binary(unsigned long val)
    {
        return get_binary_impl<true, sizeof(unsigned long)*CHAR_BIT>(val);
    }
    
    int main()
    {
        std::cout << get_binary(0) << '\n';
        std::cout << get_binary(5) << '\n';
        std::cout << get_binary<8>(0) << '\n';
        std::cout << get_binary<8>(5) << '\n';
        std::cout << get_binary(1431655765) << '\n';
        std::cout << get_binary(std::numeric_limits<unsigned long>::max()) << '\n';
    }
    Last edited by Daved; 06-19-2007 at 10:30 PM.

  13. #13
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    fun, but if you wanted to be super-anally-runtime efficient you could specialise get_binary_impl for true and false values of truncate_zeroes instead of a runtime ?:

    but that would be ridiculous
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> you could specialise get_binary_impl for true and false values
    Sounds good. Feel free to give it a go. I just spent five minutes trying and decided it's not a simple switch.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by Daved View Post
    Of course, there's no reason to do that when you can just do:
    Code:
    std::string messageOut = "header 1: test";
    In the rare case that you want to add two string literals, you can just create a temporary string out of one of them so that you are always adding to a string:
    Code:
    std::string messageOut = "header 1: " + std::string("test");
    Also, the preprocessor automatically concatenates adjacent string literals, so this
    Code:
    "hello"
    "world" "nice to"
        "meet you"
    is the same as
    Code:
    "hello world nice to meet you"
    It's useful for really long string constants. It lets you stick to an 80 character limit more easily.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [PAID] Very simple C script - Not working?
    By spadez in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-12-2009, 07:00 AM
  2. Simple program not working, don't know why
    By Bakster in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 12:56 PM
  3. Replies: 3
    Last Post: 09-12-2005, 09:08 AM
  4. Replies: 5
    Last Post: 02-02-2003, 09:56 AM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 10:36 PM

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