"Operator must be a member function..." (Error)

This is a discussion on "Operator must be a member function..." (Error) within the C++ Programming forums, part of the General Programming Boards category; Why is this invalid? Why does this have to be a member function to a class, and not a stand-alone ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    "Operator must be a member function..." (Error)

    Why is this invalid? Why does this have to be a member function to a class, and not a stand-alone function like I want?
    (String is a class I've made)
    Code:
    String operator +(const char* TempString1, const char* TempString2);
    Info :Compiling C:\Dokument\Cpp\Projekt\String\Test.cpp
    Error: STRING.H(80,69):'operator +(const char *,const char *)' must be a member function or have a parameter of class type
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Probably because you cant overload for built in types (2 char* params)

    Make 1 param a class and you will stand a better chance

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Be sure to read the error carefully, it tells you how to fix the problem.
    Code:
    String operator+ ( String TempString1, const char* TempString2 );
    -Prelude
    My best code is written with the delete key.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes, if I use the class as one parameter or if I make this a member function, then it will work. True.

    But I don't want that. I want to be able to make something like this:
    Code:
    String MyString;
    MyString = "Hello " + " World, " + " how are we" + " today?";
    And obviously, then it won't work...

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Magos
    Yes, if I use the class as one parameter or if I make this a member function, then it will work. True.

    But I don't want that. I want to be able to make something like this:
    Code:
    String MyString;
    MyString = "Hello " + " World, " + " how are we" + " today?";
    And obviously, then it won't work...
    Quoted from The C++ Programming Language 11.2.3

    An operator function must either be a member or take at least one argument of a user defined type..

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Is there any way to accomplish what I want? (see my previous post)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Update:
    I almost have it working now. BUT, I have to use parantheses around the first two items. Otherwise, the compiler thinks that I'm trying to add the addresses of the strings.

    (MyString = "Hello ") + " World, " + " how are we" + " today?";

    Is there any way to solve this, like making = have a higher priority than + ? I doubt it, but I can still hope .

    My other solution was overloading the << operator to make it function pretty much like cout. But then you have to remember to clear the string every time, since << only adds, not sets.

    (that's why I want a = and + combo).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Magos

    (MyString = "Hello ") + " World, " + " how are we" + " today?";
    I can see how that might compile (assuming the operator= returns a String), but I imagine it doesnt work as intended??

    Do you really have to implement this functionality? std::string and CString (from MFC) dont.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The above works just as intended. First, = sets the first string to the String object, then all + adds the rest of the strings.
    The problem is that if I don't use the parantheses, the compiler tries to add the adresses of the strings first, since + has a higher priority than =.
    But using the parantheses is "ugly" and I rather not do it.

    If I need this implementation? Nope, I don't need this class in the first place since there already exists string classes , but I'd really like to make my own. Sometimes I enjoy making things like this just for fun .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This isn't quite as pretty as what you have but...

    Code:
    MyString = string("Hello ") + string(" World, ") + string(" how are we") + string(" today?");
    Looking at your previous code woudn't it be better just to do this:

    Code:
    MyString = "Hello World,  how are we today?";

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but I'd really like to make my own
    That's fine, a string class is a good exercise for many levels of experience. But this particular functionality is silly, why would you want to concatenate two literal strings when this occurs anyway?

    MyString = "Hello " " World, " " how are we" " today?";

    This gives you the same result. The standard string class doesn't implement this functionality because it isn't needed. If you are going to use two string literals in a row then why not make them into one?

    MyString = "Hello World, how are we today?";

    When adding features to your class, consider long and hard whether they are actually needed or not. A useful string class really doesn't need that much, here is one I wrote recently. I split off the substring functions into another class, the end result is a surprisingly compact string class. This is because I didn't overload it with unnecessary or not often used features. Sure, it isn't a spectacular show of programming skill, but there's little point in writing a monolithic class that nobody understands:
    Code:
    namespace pre
    {
      /////////////////////////////////////
      //
      // pre::string class
      //   Basic string functionality
      //   by Julienne Walker
      //
      /////////////////////////////////////
      class string
      {
      public:
        // Public type definitions
        typedef char *iterator;
        typedef const char *const_iterator;
    
        typedef size_t size_type;
      public:
        // Object constructors and destructor
        string();
        string ( size_type, char );
        string ( const string& );
        string ( const char * );
        string ( const_iterator, const_iterator );
    
        ~string();
      public:
        // Subscript access operators
        char& operator[] ( size_type );
        const char& operator[] ( size_type ) const;
      public:
        // Assignment operators
        string& operator= ( const string& );
        string& operator= ( const char * );
      public:
        // Concatenation assignment operators
        string& operator+= ( const string& );
        string& operator+= ( const char * );
      public:
        // Concatenation operators
        string operator+ ( const string& );
        string operator+ ( const char * );
      public:
        // Public user interface
        void push_back ( const char& );
    
        void clear();
        void clear_all();
    
        size_type size() const;
        size_type length() const;
    
        const char *c_str();
        const char *data();
    
        iterator begin();
        const_iterator begin() const;
    
        iterator end();
        const_iterator end() const;
    
        bool compare ( const string& ) const;
        bool compare ( const char * ) const;
      private:
        // Memory handling functions
        void build_mem();
        void build_mem ( size_type, const char& );
        void build_mem ( const_iterator, const_iterator );
    
        void unbuild_mem();
    
        void resize();
      private:
        // Memory handling variables
        iterator str_data; // Beginning of string
        iterator str_end;  // End of string
        iterator mem_end;  // End of memory allocated to string
    
        std::allocator<char> alloc;
      };
    }
    
    // Global string operators and functions
    std::istream& operator>> ( std::istream&, pre::string& );
    std::ostream& operator<< ( std::ostream&, pre::string );
    
    std::istream& getline ( std::istream&, pre::string& );
      
    bool operator== ( const pre::string&, const char * );
    bool operator== ( const pre::string&, const pre::string& );
    bool operator!= ( const pre::string&, const char * );
    bool operator!= ( const pre::string&, const pre::string& );
    -Prelude
    My best code is written with the delete key.

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Prelude
    But this particular functionality is silly, why would you want to concatenate two literal strings when this occurs anyway?
    It doesn't occur for variables.
    I could be useful to overload all default operators, not just operator new and delete. But it could cause much trouble too, and that's probably why it isn't allowed.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Magos
    The above works just as intended. First, = sets the first string to the String object, then all + adds the rest of the strings.
    Then I think your string class is a bit strange. IMO, operator + shouldn't change anything.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Sang-drax
    Then I think your string class is a bit strange. IMO, operator + shouldn't change anything.
    I agree, but earlier in my post it was stated that I couldn't use an overloaded function with two char array arguments, returning a class.

    Also, the ability to add several strings together like this:
    String = "Hello" + "World";
    is kinda pointless, I agree. But if you want variables too:
    Code:
    int Age = 21;
    char* Name = "Magos";
    String Occupation("Student");
    
    String Me;
    Me = "Hello " + Name + ", are you " + Age + " years old? Oh, you're a " + Occupation + "!";
    Then this kind of concatenation is more useful.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I agree, but think what will happen in a programming team when someone suddenly overloads the default operators.

    Example:
    You want to be able to add a char* and a int like this:
    Code:
    int age = 5;
    MyString str = "I am " + age + " years old.";
    But someone else is using this code
    Code:
    char* ptr = ...;
    while ( *ptr != 'a' and *(ptr+1) ) ptr++;
    Problem.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

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

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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