What constructor or overloaded operator= am I missing?

From: William Payne (mikas_n_o_s_p_a_m_493_at_student.liu.se)
Date: 03/31/04


Date: Wed, 31 Mar 2004 18:30:31 +0200

Hello, using my simple string class called String, I noticed I cannot do:
String s = "hi";
without getting the following compilation error:
error: conversion from `const char[3]' to non-scalar type `String' requested

Here's the class declaration for String, what have I forgotten to implement
in order for the above line of code to compile:

class String
{
public:
   String()
      :
      m_internal_string(NULL),
      m_length(0)
   {
      ; /* Do nothing. */
   }

   explicit String(const char* s);

   explicit String(const String& s);

   virtual ~String()
   {
      delete [] m_internal_string;
   }

   size_t length() const
   {
      return m_length;
   }

   const char* c_str() const
   {
      return m_internal_string;
   }

   void clear();

   char operator[](std::size_t index) const;
   String& operator+=(const char* s);
   String& operator+=(const String& s);
   String& operator=(const char* s);
   String& operator=(const String& rhs);
   bool operator==(const String& rhs);
   friend std::ostream& operator<<(std::ostream& os, const String& s);
   friend std::istream& operator>>(std::istream& is, String& s);
private:
   char* m_internal_string;
   std::size_t m_length;
};

Thanks for any replies

/ William Payne



Relevant Pages

  • Re: So many string types!
    ... > complains that it can not convert CHString to LPSTR. ... other .NET-languages only have one string type. ... AFAIK fopen expects a const char*, ... string class I know has a conversion to one of these types. ...
    (microsoft.public.dotnet.general)
  • RE: Calendar code error.
    ... "Jim Thomlinson" wrote: ... Compilation Error: Method or data member not found. ... Shift As Integer, ByVal CallbackField As String, CallbackDate As Date) ...
    (microsoft.public.excel.programming)
  • RE: Calendar code error.
    ... Compilation Error: Method or data member not found. ... Private Sub DateArrived_CallbackKeyDown(ByVal KeyCode As Integer, ... Shift As Integer, ByVal CallbackField As String, CallbackDate As Date) ...
    (microsoft.public.excel.programming)
  • Re: asp:hyperlink object in Repeater control ??
    ... but when the page tries to load I get the following compilation error; ... > Function MakeURL(ByVal strURL As String) As String ... > Function CreateDataSource() As DataTable ... > Dim dt As New DataTable ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: String constants "constness"
    ... As Ulrich already pointed, compiler implicitly converts string ... danger of this conversion you demonstrated by yourself. ... Technically, the string literal IS const, but its address can still be ...
    (microsoft.public.vc.language)