Programmer
Programmer

Reputation: 105

must be a nonstatic member function

Trying to compile some code and I keep getting an error that says " 'myString& operator=(const myString&)' must be a nonstatic member function"

This is my namelist.cpp

myString& operator=(const myString& string)
  {
          if(this = &string)
                  return *this;

          data = new char[strlen(string.data)+1];

          strcpy(data, string.data);
        length = string.length;

          return *this;
  }

This is some of my namelist.h

  9 class myString
 10 {
 11 private:
 12         char* data;
 13         int length;
 14
 15 public:
 16         myString();
 17         myString(char cString[]);
 18         myString(myString& cString);
 19         //desctructor
 20         myString operator=(const myString& string);
 21         myString operator+(const myString& string);

Upvotes: 0

Views: 4169

Answers (1)

John Zwinck
John Zwinck

Reputation: 249103

The declaration should be:

myString& operator=(const myString& string);

And the definition:

myString& myString::operator=(const myString& string)

Upvotes: 1

Related Questions