Operator overloading in C++

Sebastian Faubel s.faubel@web.de
Mon Apr 7 22:52:00 GMT 2008


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



More information about the Gcc-help mailing list