Thread: Simple problem...seems like it should work

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    46

    Simple problem...seems like it should work

    I think I may be confussing the difference between strings and string literals.

    I know this works:
    Code:
    const string hello = "Hello";
    const string greeting = hello + ", world" + "!";
    but if I try this:

    Code:
    const string explan = "!";
    const string greeting = "Hello" + ", world" + explan;
    I get a compiler error. I'm not sure I follow why this doesn't work. I've tried a bunch of combinations, but it won't let me do the above.

    Why is this illegal?

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    My guess is that the compiler checks the equation from left to right. On the first case you have std::string + const char* which is overloaded and returns a reference to a string which is the part of a new addition and then returns a string which will be inserted in "greeting".

    However, in the case below since you are doing the opposite you start with const char* + const char* which makes no sense hense the compiler complaining.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Alexandre is correct: a const char is not an object and has no + operator associated with it, whereas a string does, and an anonymous string literal is a const char type, not a string object.

    Ie. it's the same reason you cannot do this in C++:

    Code:
    int len = "Hello".size();
    but

    Code:
    int len = strlen("Hello");
    is okay.
    Last edited by MK27; 09-01-2011 at 09:35 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    46
    Thanks guys makes total sense now

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    But why? In my "CVector" class, both "scalar*vector" and "vector*scalar" can be expressed, by using a friend global operator that reverses the order of the operands!
    Devoted my life to programming...

  6. #6
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by Sipher View Post
    But why? In my "CVector" class, both "scalar*vector" and "vector*scalar" can be expressed, by using a friend global operator that reverses the order of the operands!
    You can't overload operators for native types.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Sorry, when I said "scalar", I didn't mean another class but just a "float".
    Devoted my life to programming...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, but you can't overload operator + (float, float), which is what the issue is about.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple mod function won't work
    By cb0ardpr0gr^mm3r in forum C Programming
    Replies: 7
    Last Post: 06-18-2011, 07:07 PM
  2. Getting a Very Simple Quake 2 Bot to Work
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2009, 05:36 AM
  3. Simple C++ Won't Work...
    By jothesmo in forum C++ Programming
    Replies: 10
    Last Post: 11-25-2005, 07:00 PM
  4. My function seems too simple to work..
    By face_master in forum C++ Programming
    Replies: 10
    Last Post: 05-27-2002, 02:58 PM
  5. Why doesn't this work? (simple)
    By Clyde in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 04:48 PM