Thread: c++ - adding 2 const char*

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    c++ - adding 2 const char*

    if:
    Code:
    string a = "hello" + "hi";
    why i can't:

    Code:
    string &operator+(const char *s1, const char *s2)
    {
        string a=(string)s1+s2;
        return a;
    }
    ???
    heres the error message:
    "'std::string operator+(const char*, const char*)' must have an argument of class or enumerated type"

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    That operator overload must be part of a class, you can't create an overloaded operator without the class.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by jimblumberg View Post
    That operator overload must be part of a class, you can't create an overloaded operator without the class.

    Jim
    but the operator+() only accept 1 or 2 arguments.. and not 3.
    my objective, with my new class is avoiding the casting:
    Code:
    String d="hello" + "hi";
    for works, i must casting the 1st const char*... but i want avoid that.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If it is part of a class, please show the complete class definition and implementation.


    Jim

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by jimblumberg View Post
    If it is part of a class, please show the complete class definition and implementation.


    Jim
    heres the class and some samples:
    Code:
    class String
    {
        private:
    
        public:
            string b="";
        String(string s="")
        {
            b=s;
        }
    
        String(const char *s="")
        {
            b=s;
        }
    
        String& operator=(const string &s)
        {
            b=s;
            return *this;
        }
    
        String& operator=(const String &s)
        {
            b=s.b;
            return *this;
        }
        operator string()
        {
            return b;
        }
    
    };
    
    //by some reason: these 2 functions must be outside of the class and the 'b' must be public and not private
    String& operator+(String &s1, const char *s2)
    {
        s1.b += s2;
        return s1;
    }
    
    String& operator+(String &s1, String &s2)
    {
        s1.b += s2.b;
        return s1;
    }
    
    String test="hi";
    String test2=" hi";
    String test3=test+test2 + "hello";
    String test4 =(string) "hi" + "hello";
    like you see: the test4 must have the string casting, but i want avoid that.
    that's why i was trying do that function:
    Code:
    string &operator+(const char *s1, const char *s2)
    {
        string a=(string)s1+s2;
        return a;
    }
    isn't a String type return... but you see what i mean

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    To overload a binary operator (+, -, etc), at least one side of the operator must be a user-defined type. This is an immutable rule of C++.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by Elkvis View Post
    To overload a binary operator (+, -, etc), at least one side of the operator must be a user-defined type. This is an immutable rule of C++.
    like you see, i have it:
    Code:
    String& operator+(String &s1, const char *s2)
    but not works with:
    Code:
    String test4 =(string) "hi" + "hello";

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by joaquim View Post
    Code:
    String test4 =(string) "hi" + "hello";
    You can't just cast a string constant to a string class object. You can use construction syntax:

    Code:
    string test4 = string("hi") + "hello";
    but a C-style cast will not work.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    String& operator+(String &s1, const char *s2)
    {
        s1.b += s2;
        return s1;
    }
     
    String& operator+(String &s1, String &s2)
    {
        s1.b += s2.b;
        return s1;
    }
    operator + shall not modify its arguments.
    I imagine you would it pretty strange if:

    Code:
    int x = 10;
    int y = 5;
    int z = x + y;
    Results in x = 15, y = 5, z = 15.
    But that's precisely what your code would do to a string object.
    Furthermore, you must never return a reference to a temporay.
    So the correct version would be:

    Code:
    String operator + (const String& s1, const char* s2)
    {
        String tmp(s1);
        tmp.b += s2;
        return s1;
    }
    Last edited by Elysia; 09-30-2014 at 11:01 AM. Reason: Fixed typo
    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.

  10. #10
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Elysia View Post
    So the correct version would be:

    Code:
    String operator + (const String& s1, const char* s2)
    {
        String tmp(s1);
        s1.b += s2;
        return s1;
    }
    You made a tiny but important typo there. I think you meant:

    Code:
    tmp.b += s2;
    return tmp;

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are correct. Thanks for finding the typo =) I've updated the original post.
    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.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I realize that some people are going to hate the suggestion, but I always wonder why people prefer concatenation over direct construction.

    I get it; I do! That variadic constructor is unusual. You often want the concatenation operators in any event. (You want them for other uses.) The usage makes certain purists cry. In the case of this thread, problems need to be understood and solve in any event.

    All of that completely ignored, why don't people offer variadic constructor for such situations!?

    Code:
    string a("hello", "hi"); // = /* string a = string("hello") + "hi"; */ ~= /* string a = "hello" + "hi"; */
    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you just want to concatenate string literals, you can do so without putting any operator between them. This is useful for multi-line strings.

    Code:
    const auto str = "hello " 
        "there";
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by joaquim View Post
    if:
    Code:
    string a = "hello" + "hi";
    Firstly, that sort of addition is not possible - the addition operator is not applicable to string literals - so the rest of your question is invalid.

    Asking a question "If X then why not Y?" when X is always false is pointless.

    However, to clarify the error message from your compiler ....

    Quote Originally Posted by joaquim View Post
    why i can't:

    Code:
    string &operator+(const char *s1, const char *s2)
    {
        string a=(string)s1+s2;
        return a;
    }
    ???
    heres the error message:
    "'std::string operator+(const char*, const char*)' must have an argument of class or enumerated type"
    The error message is self explanatory. At least one of the operands (i.e. the things being added if we're talking about addition) for any overloaded operator must be a user-defined type. Overloading operators for basic types (int, char, pointers to them, etc) is not permitted.

    I've never seen the reason formally explained, but I assume it is to prevent programmers doing dumb things like overloading operators to make an expression like "2 + 3" give a result of 6701 (i.e. to make addition of basic types like int misbehave).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM