Overloaded operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shlainn
    New Member
    • Mar 2008
    • 2

    Overloaded operator

    I've got a problem trying to compile a program written in Visual C++ in MinGW/GCC. While it works in VC++, MinGW dies on this piece of code:

    Code:
            ByteBuffer &operator<<(const std::string &value)
            {
    	    append((uint8 *)value.c_str(), value.length());
                append((uint8)0);
                return *this;
            }
    with this error:
    Code:
    ./shared/ByteBuffer.h: In member function `ByteBuffer& ByteBuffer::operator<<(const std::string&)':
    ./shared/ByteBuffer.h:112: error: invalid use of undefined type `const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
    C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stringfwd.h:56: error: declaration of `const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
    ./shared/ByteBuffer.h:112: error: invalid use of undefined type `const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
    C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stringfwd.h:56: error: declaration of `const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
    I don't have any idea what could be wrong (I'm kinda new to this, as you may have guessed), so any help is appreciated.

    Regards,

    shlainn
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Did you #include<string >? Also, the << and >> operators cannot be class member functions since their first input must be an istream& or ostream&, depending, rather than the invisible 'this'. They can be friend functions (and if you want to associate them with a class, should be), but they can't be member functions.

    Comment

    • shlainn
      New Member
      • Mar 2008
      • 2

      #3
      Did you #include<string >?
      Actually I thought so. But when I checked it wasn't there. Stupid me,

      thanks for the heads up.

      Shlainn

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        How are you getting this to work?

        operator<< requires two arguments while your function shows only one.

        Plus, in order to overload an operator one of the arguments must be a user-defined type. Yours uses str::string.

        Are you sure this ever compiled?

        Comment

        Working...