This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Operator overloading in C++
- From: Sebastian Faubel <s dot faubel at web dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 7 Apr 2008 23:26:40 +0200
- Subject: Operator overloading in C++
hi there,
i have a strange problem with g++, which i couldn't figure out myself so far:
I am currently working on a string class, which is directly derived from std::basic_string. Since I am writing a wrapper API for a C library which uses unsigned char* strings a lot, this makes sense to me. For this I want to have operators overloaded for const char* AND unsigned char* which are provided by the base class. I have defined and implemented these operators in the string class header, like it is in the STL header for std::basic_string -- but somehow g++ fails find those custom operators ( i.e. operator.+= ). The compile error shows that it's only looking for the operators in the STL header. What am I missing out?
thanks a lot! :)
~sebastian
****
Class definition
----------------------------------------------------------------------------------------------------------------------------
namespace rdf
{
//! Redland uses unsigned char to ensure the type has 8 bits for UTF-8.
typedef unsigned char uchar;
//! Derive a Redland compatible string class from std::basic_string.
class unsigned_string : public std::basic_string<uchar>
{
public:
unsigned_string() :
std::basic_string<uchar>()
{}
unsigned_string(const char* __s) :
std::basic_string<uchar>((const uchar*) __s)
{}
unsigned_string(const uchar* __s) :
std::basic_string<uchar>(__s)
{}
unsigned_string(const unsigned_string& __s) :
std::basic_string<uchar>(__s)
{}
unsigned_string& append(const unsigned_string& __s)
{
return append(__s);
}
unsigned_string& append(const string& __s)
{
return append((const unsigned_string&) __s);
}
unsigned_string& append(const char* __s)
{
return append((const uchar*) __s);
}
unsigned_string& operator=(const char* __s)
{
return (unsigned_string&) assign((const uchar*) __s);
}
unsigned_string& operator+=(const char* __s)
{
return append((const uchar*) __s);
}
operator const uchar*()
{
return c_str();
}
};
//! Redland C++ enables std::string like handling of unsigned char strings.
typedef unsigned_string ustring;
}
Compiler output:
----------------------------------------------------------------------------------------------------------------------------
error: ambiguous overload for âoperator+=â in âu1 += " "â
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:757: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator+=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = unsigned char, _Traits = std::char_traits<unsigned char>, _Alloc = std::allocator<unsigned char>] <near match>
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:766: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator+=(const _CharT*) [with _CharT = unsigned char, _Traits = std::char_traits<unsigned char>, _Alloc = std::allocator<unsigned char>] <near match>
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:775: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator+=(_CharT) [with _CharT = unsigned char, _Traits = std::char_traits<unsigned char>, _Alloc = std::allocator<unsigned char>] <near match>
make[2]: *** [test/CMakeFiles/ustring.dir/ustring.o]
Error 1 make[1]: *** [test/CMakeFiles/ustring.dir/all]
Error 2 make: *** [all] Error 2