title: Error with overloading operator in string project [SOLVED]
body: <p>I am trying to implement my overloading operator for this string project I am working on but I keep getting these following errors when running my program:</p>
  <blockquote>
  <p>undefined reference to `cs_mystring::MyString::operator=(cs_mystring::MyString const&amp;)'|</p>
  <p>undefined reference to `cs_mystring::MyString::MyString(cs_mystring::MyString const&amp;)'|</p>
  </blockquote>
  <p>Here is my header file:</p>
  <pre><code>#ifndef MYSTRING_H
  #define MYSTRING_H
  
  #include &lt;fstream&gt;
  #include &lt;cctype&gt;      // for toupper()
  #include &lt;string&gt;
  #include &lt;cassert&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  namespace cs_mystring
  {
      class MyString
      {
  
      public:
  
          static const int MAX_INPUT_SIZE = 127;
  
          int length() const;
          MyString();  // default constructor
          MyString(const char* inString); // parameterized constructor
  
          friend std::ostream&amp; operator &lt;&lt; (std::ostream &amp;out, const MyString&amp; printMe);
          friend std::istream&amp; operator &gt;&gt; (std::istream&amp; in, MyString&amp; readMe);
          void readline(std::istream&amp; in, char delimeter); //another setter type function
  
          char operator[] (int index) const;
          char&amp; operator [] (int index);
  
          friend bool operator &lt; (const MyString&amp; leftOp, const MyString&amp; rightOp);
          friend bool operator &lt;= (const MyString&amp; leftOp, const MyString&amp; rightOp);
          friend bool operator &gt; (const MyString&amp; leftOp, const MyString&amp; rightOp);
          friend bool operator &gt;= (const MyString&amp; leftOp, const MyString&amp; rightOp);
          friend bool operator == (const MyString&amp; leftOp, const MyString&amp; rightOp);
          friend bool operator != (const MyString&amp; leftOp, const MyString&amp; rightOp);
          MyString operator += (const MyString&amp; rightOp);
          friend MyString operator + (const MyString&amp; leftOp, const MyString&amp; rightOp);
  
          ~MyString();  // class destructor
          MyString operator=(const MyString&amp; right);  // = operator used in client code
          MyString(const MyString&amp; copyMe);  //copy constructor makes a deep copy
  
      private:
  
          char *str;
      };
  }
  
  #endif
  </code></pre>
  <p>My cpp file:</p>
  <pre><code>#include &quot;mystring.h&quot;
  #include &lt;fstream&gt;
  #include &lt;cctype&gt;  
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  #include &lt;cassert&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  namespace cs_mystring
  {
  
  
  MyString::MyString()  // default constructor
  {
      str = new char[1];
      strcpy(str, &quot;&quot;);
  }
  
  MyString::MyString(const char* inString) // parameterized constructor
  {
      str = new char[strlen(inString) + 1];
      strcpy(str, inString);
  }
  
  MyString::~MyString()
  {
      delete [] str;
  }
  
  ostream&amp; operator &lt;&lt; (std::ostream &amp;out, const MyString&amp; printMe)
  {
          out &lt;&lt; printMe.str;
          return out;
  }
  
  istream&amp; operator &gt;&gt; (std::istream&amp; in, MyString&amp; readMe)
  {
          delete[] readMe.str;
          char tempString[MyString::MAX_INPUT_SIZE + 1];
          in &gt;&gt; tempString;
          readMe.str = new char[strlen(tempString) + 1];
          strcpy(readMe.str, tempString);
          return in;
  }
  
  void MyString::readline(std::istream&amp; in, char delimeter)
  {
  
          char tempStr[MyString::MAX_INPUT_SIZE + 1];
          in.getline(tempStr, MyString::MAX_INPUT_SIZE + 1, delimeter);
          delete [] str;
          str = new char[strlen(tempStr) + 1];
          strcpy(str, tempStr);
  }
  
  int MyString::length() const
  {
      return strlen(str);
  }
  
  char MyString::operator[](int index)const
  {
      assert(index &gt;= 0 &amp;&amp; index &lt; strlen(str));
      return str[index];
  }
  
  char&amp; MyString::operator[](int index)
  {
      assert(index &gt;= 0 &amp;&amp; index &lt; strlen(str));
      return str[index];
  }
  
  bool operator &lt; (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      return strcmp(leftOp.str, rightOp.str) &lt; 0;
  }
  
  bool operator &lt;= (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      return strcmp(leftOp.str, rightOp.str) &lt;= 0;
  }
  
  bool operator &gt; (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      return strcmp(leftOp.str, rightOp.str) &gt; 0;
  }
  
  bool operator &gt;= (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      return strcmp(leftOp.str, rightOp.str) &gt;= 0;
  }
  
  bool operator == (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      return strcmp(leftOp.str, rightOp.str) == 0;
  }
  
  bool operator != (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      return strcmp(leftOp.str, rightOp.str) != 0;
  }
  
  MyString operator + (const MyString&amp; leftOp, const MyString&amp; rightOp)
  {
      MyString tempString;
      delete [] tempString.str;
      tempString.str = new char[strlen(leftOp.str) + strlen(rightOp.str) + 1];
      strcpy(tempString.str, leftOp.str);
      strcat(tempString.str, rightOp.str);
  
      return tempString;
  
  
  }
  
  MyString MyString::operator += (const MyString&amp; rightOp)
  {
      *this = *this + rightOp;
  
      return *this;
  }
  
  /*MyString MyString::operator=(const MyString&amp; right)
  {
      if (this != &amp;right)
      {
      delete[] str;
      str = new char[strlen(right.str) + 1];
      strcpy(str, right.str);
      }
  
          return *this;
  } */
  
  }
  </code></pre>
  <p>my main file using everything in my class:</p>
  <pre><code> #include &quot;mystring.h&quot;
   #include &lt;fstream&gt;
   #include &lt;cctype&gt;     
   #include &lt;string&gt;
   #include &lt;cassert&gt;
   #include &lt;iostream&gt;
   using namespace std;
   using namespace cs_mystring;
  
  void BasicTest();
  void RelationTest();
  void ConcatTest();
  void CopyTest();
  MyString AppendTest(const MyString&amp; ref, MyString val);
  string boolString(bool convertMe);
  
  int main()
  {
      cout &lt;&lt; &quot;Project#4: MyString class (ADT) client program to test CLASS INVARIANT\n&quot;;
      cout &lt;&lt; &quot;The class has one private data member defined as follows:\n&quot;;
      cout &lt;&lt; &quot;char *str;\n&quot;;
      cout &lt;&lt; &quot;str always represents a valid null-terminated c-string for each instance\n&quot;;
      cout &lt;&lt; &quot;of a MyString class object class.\n&quot;;
      cout &lt;&lt; &quot;The client code provides an interface to test correct operations on MyString\n&quot;;
      cout &lt;&lt; &quot;objects\n&quot;;
  
      cout &lt;&lt; &quot;\nHere is a list of tests and the order in which the tests will be conducted\n&quot;;
      cout &lt;&lt; &quot;1. BasicTest\n&quot;;
      cout &lt;&lt; &quot;2. RelationTest\n&quot;;
      cout &lt;&lt; &quot;3. ConcatTest\n&quot;;
      cout &lt;&lt; &quot;4. CopyTest\n\n&quot;;
  
      BasicTest();
      RelationTest();
      ConcatTest();
     /* CopyTest(); */
  
      cout &lt;&lt; &quot;\nProject#4 -MyString class (ADT) testing now concluded.\n&quot;;
      cout &lt;&lt; &quot;MyString class destructor has been invoked and memory from the heap deallocated\n&quot;;
      cout &lt;&lt; &quot;Check output for correct results from the MyString class (ADT) implementation.\n&quot;;
  
  
      return 0;
  }
  
  
  string boolString(bool convertMe) {
      if (convertMe) {
          return &quot;true&quot;;
      } else {
          return &quot;false&quot;;
      }
  }
  
  
  void BasicTest()
  {
      MyString s;
      int stringLength;
  
      cout &lt;&lt; &quot;***********************************************************************\n&quot;;
      cout &lt;&lt; &quot;* Basic Test: Testing various member constructor options and nonmember*\n&quot;;
      cout &lt;&lt; &quot;* friend ostream &lt;&lt; operator for basic MyString object creation &amp;     *\n&quot;;
      cout &lt;&lt; &quot;* printing                                                            *\n&quot;;
      cout &lt;&lt; &quot;***********************************************************************\n&quot;;
  
      const MyString strs[] =
                  {MyString(&quot;Wow&quot;), MyString(&quot;C++ is neat!&quot;),
                   MyString(&quot;&quot;), MyString(&quot;a-z&quot;)};
  
  
      for (int i = 0; i &lt; 4; i++){
          cout &lt;&lt; &quot;string [&quot; &lt;&lt; i &lt;&lt;&quot;] = &quot; &lt;&lt; strs[i] &lt;&lt; endl;
      }
  
      cout &lt;&lt; &quot;\n***********************************************************************\n&quot;;
      cout &lt;&lt; &quot;* Basic Test: Testing nonmember friend istream &gt;&gt; and ostream &lt;&lt;      *\n&quot;;
      cout &lt;&lt; &quot;* operators for reading and display of MyString objects from data file*\n&quot;;
      cout &lt;&lt; &quot;***********************************************************************\n&quot;;
  
      cout &lt;&lt; endl &lt;&lt; &quot;----- first, word by word&quot; &lt;&lt; endl;
      ifstream in(&quot;mystring_data.txt&quot;);
      assert(in);
      while (in.peek() == '#'){
          in.ignore(128, '\n');
      }
      in &gt;&gt; s;
      while (in) {
          cout &lt;&lt; &quot;Read string = &quot; &lt;&lt; s &lt;&lt; endl;
          in &gt;&gt; s;
      }
      in.close();
  
      cout &lt;&lt; &quot;\n*************************************************************************\n&quot;;
      cout &lt;&lt; &quot;* Basic Test: Testing member function readline read, a nonmember friend *\n&quot;;
      cout &lt;&lt; &quot;* istream &gt;&gt; and ostream &lt;&lt;operators for reading and display of MyString*\n&quot;;
      cout &lt;&lt; &quot;* objects from data file                                                *\n&quot;;
      cout &lt;&lt; &quot;* ***********************************************************************\n&quot;;
  
      cout &lt;&lt; endl &lt;&lt; &quot;----- now, line by line&quot; &lt;&lt; endl;
      ifstream in2(&quot;mystring_data.txt&quot;);
      assert(in2);
      while (in2.peek() == '#'){
          in2.ignore(128, '\n');
      }
      s.readline(in2, '\n');
      while (in2) {
          cout &lt;&lt; &quot;Read string = &quot; &lt;&lt; s &lt;&lt; endl;
          s.readline(in2, '\n');
      }
      in2.close();
  
      cout &lt;&lt; &quot;\n****************************************************************************\n&quot;;
      cout &lt;&lt; &quot;* Basic Test: Testing access to characters (using const and non const)     *\n&quot;;
      cout &lt;&lt; &quot;* using constructors, member function length that returns a cstring length,*\n&quot;;
      cout &lt;&lt; &quot;* the square bracket [] overloaded operator and a nonmember friend         *\n&quot;;
      cout &lt;&lt; &quot;* istream &gt;&gt; for display of MyString objects content                       *\n&quot;;
      cout &lt;&lt; &quot;* **************************************************************************\n&quot;;
  
      cout &lt;&lt; endl &lt;&lt; &quot;----- Testing access to characters (using const)&quot; &lt;&lt; endl;
      const MyString s1(&quot;abcdefghijklmnopqsrtuvwxyz&quot;);
      cout &lt;&lt;  &quot;Whole string is &quot; &lt;&lt; s1 &lt;&lt; endl;
      cout &lt;&lt; &quot;now char by char: &quot;;
      stringLength = s1.length();
      for (int i = 0; i &lt; stringLength; i++){
          cout &lt;&lt; s1[i];
      }
  
  
      cout &lt;&lt; endl &lt;&lt; &quot;\n----- Testing access to characters (using non-const)&quot; &lt;&lt; endl;
      MyString s2(&quot;abcdefghijklmnopqsrtuvwxyz&quot;);
      cout &lt;&lt;  &quot;Start with &quot; &lt;&lt; s2;
      stringLength = s2.length();
      for (int i = 0; i &lt; stringLength; i++){
          s2[i] = toupper(s2[i]);
      }
      cout &lt;&lt; &quot; and convert to &quot; &lt;&lt; s2 &lt;&lt; endl;
  }
  
  
  void RelationTest()
  {
  
      cout &lt;&lt; &quot;\n***********************************************************************\n&quot;;
      cout &lt;&lt; &quot;* RelationTest: Testing nonmember friend binary Boolean &lt;, &lt;=, &gt;, &gt;=, *\n&quot;;
      cout &lt;&lt; &quot;*  ==, != relational operators between MyString objects               *\n&quot;;
      cout &lt;&lt; &quot;***********************************************************************\n&quot;;
  
      const MyString strs[] =
          {MyString(&quot;app&quot;), MyString(&quot;apple&quot;), MyString(&quot;&quot;),
          MyString(&quot;Banana&quot;), MyString(&quot;Banana&quot;)};
  
      for (int i = 0; i &lt; 4; i++) {
          cout &lt;&lt; &quot;Comparing &quot; &lt;&lt; strs[i] &lt;&lt; &quot; to &quot; &lt;&lt; strs[i+1] &lt;&lt; endl;
          cout &lt;&lt; &quot;\tIs left &lt; right? &quot; &lt;&lt; boolString(strs[i] &lt; strs[i+1]) &lt;&lt; endl;
          cout &lt;&lt; &quot;\tIs left &lt;= right? &quot; &lt;&lt; boolString(strs[i] &lt;= strs[i+1]) &lt;&lt; endl;
          cout &lt;&lt; &quot;\tIs left &gt; right? &quot; &lt;&lt; boolString(strs[i] &gt; strs[i+1]) &lt;&lt; endl;
          cout &lt;&lt; &quot;\tIs left &gt;= right? &quot; &lt;&lt; boolString(strs[i] &gt;= strs[i+1]) &lt;&lt; endl;
          cout &lt;&lt; &quot;\tDoes left == right? &quot; &lt;&lt; boolString(strs[i] == strs[i+1]) &lt;&lt; endl;
          cout &lt;&lt; &quot;\tDoes left != right ? &quot; &lt;&lt; boolString(strs[i] != strs[i+1]) &lt;&lt; endl;
      }
  
  
      cout &lt;&lt; &quot;\n***********************************************************************\n&quot;;
      cout &lt;&lt; &quot;* RelationTest: Testing nonmember friend binary Boolean &lt;, &lt;=, &gt;, &gt;=, *\n&quot;;
      cout &lt;&lt; &quot;*  ==, != relational operators between MyString objects and char      *\n&quot;;
      cout &lt;&lt; &quot;***********************************************************************\n&quot;;
  
      MyString s(&quot;he&quot;);
      const char *t = &quot;hello&quot;;
      cout &lt;&lt; &quot;Comparing &quot; &lt;&lt; s &lt;&lt; &quot; to &quot; &lt;&lt; t &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &lt; right? &quot; &lt;&lt; boolString(s &lt; t) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &lt;= right? &quot; &lt;&lt; boolString(s &lt;= t) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &gt; right? &quot; &lt;&lt; boolString(s &gt; t) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &gt;= right? &quot; &lt;&lt; boolString(s &gt;= t) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tDoes left == right? &quot; &lt;&lt; boolString(s == t) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tDoes left != right ? &quot; &lt;&lt; boolString(s != t) &lt;&lt; endl;
  
      MyString u(&quot;wackity&quot;);
      const char *v = &quot;why&quot;;
      cout &lt;&lt; &quot;Comparing &quot; &lt;&lt; v &lt;&lt; &quot; to &quot; &lt;&lt; u &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &lt; right? &quot; &lt;&lt; boolString(v &lt; u) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &lt;= right? &quot; &lt;&lt; boolString(v &lt;= u) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &gt; right? &quot; &lt;&lt; boolString(v &gt; u) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tIs left &gt;= right? &quot; &lt;&lt; boolString(v &gt;= u) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tDoes left == right? &quot; &lt;&lt; boolString(v == u) &lt;&lt; endl;
      cout &lt;&lt; &quot;\tDoes left != right ? &quot; &lt;&lt; boolString(v != u) &lt;&lt; endl;
  
  }
  
  
  void ConcatTest()
  {
      cout &lt;&lt; &quot;\n************************************************************************\n&quot;;
      cout &lt;&lt; &quot;* ConcatTest: Testing nonmember friend binary arithmetic + operator    *\n&quot;;
      cout &lt;&lt; &quot;* that do concatenation on MyString objects                            *\n&quot;;
      cout &lt;&lt; &quot;************************************************************************\n&quot;;
  
      cout &lt;&lt; &quot;\n----- Testing concatenation on MyStrings\n&quot;;
      const MyString s[] =
              {MyString(&quot;outrageous&quot;), MyString(&quot;milk&quot;), MyString(&quot;&quot;),
              MyString(&quot;cow&quot;), MyString(&quot;bell&quot;)};
  
      for (int i = 0; i &lt; 4; i++) {
          cout &lt;&lt; s[i] &lt;&lt; &quot; + &quot; &lt;&lt; s[i+1] &lt;&lt; &quot; = &quot; &lt;&lt; s[i] + s[i+1] &lt;&lt; endl;
      }
  
      cout &lt;&lt; &quot;\n************************************************************************\n&quot;;
      cout &lt;&lt; &quot;* ConcatTest: Testing nonmember friend binary arithmetic + operator    *\n&quot;;
      cout &lt;&lt; &quot;* that do concatenation between MyString objects and char*             *\n&quot;;
      cout &lt;&lt; &quot;************************************************************************\n&quot;;
  
      cout &lt;&lt; &quot;\n----- Testing concatenation between MyString and char *\n&quot;;
      const MyString a(&quot;abcde&quot;);
      const char *b = &quot;XYZ&quot;;
      cout &lt;&lt; a &lt;&lt; &quot; + &quot; &lt;&lt; b &lt;&lt; &quot; = &quot; &lt;&lt; a + b &lt;&lt; endl;
      cout &lt;&lt; b &lt;&lt; &quot; + &quot; &lt;&lt; a &lt;&lt; &quot; = &quot; &lt;&lt; b + a &lt;&lt; endl;
  
  
      cout &lt;&lt; &quot;\n************************************************************************\n&quot;;
      cout &lt;&lt; &quot;* ConcatTest: Testing member binary arithmetic += operator             *\n&quot;;
      cout &lt;&lt; &quot;* that do concatenation between MyString objects and char*             *\n&quot;;
  
  
      cout &lt;&lt; &quot;\n----- Testing shorthand concat/assign using MyString and 
    char *\n&quot;;
      MyString u(&quot;I love &quot;);
      const char *v = &quot;programming&quot;;
      cout &lt;&lt; u &lt;&lt; &quot; += &quot; &lt;&lt; v &lt;&lt; &quot; = &quot;;
      cout &lt;&lt; (u += v) &lt;&lt; endl;
  }
  </code></pre>
  <p>I can provide the rest of the code if needed</p>