Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It simply means that this.substr() is not valid C++. this is a pointer to the current object. So unless the method MyString::substr() exists, you can't do that.</p> <p>Starting from that, I don't know which members there are in your MyString class. If there is a std::string, you can use the substr method on it. f the underlying member is just a char*, you will have to use simple array and c-string operations on it to extract a subtring from it.</p> <p>With an exemple. If your class is like this</p> <pre><code>class MyString { private: std::string _str; public: // Constructor MyString(std::string str); // Your method MyString Substring(int start, int length) const; }; </code></pre> <p>Then your Substring method will be like this:</p> <pre><code>MyString MyString::Substring(int start, int length) const { return MyString(_str.substr(start, length)); } </code></pre> <p>On the other hand, if your class MyString is like that:</p> <pre><code>class MyString { private: char* _str; public: // Constructor MyString(char* str); // Your method MyString Substring(int start, int length) const; }; </code></pre> <p>Then your method will be like this:</p> <pre><code>MyString MyString::Substring(int start, int length) const { char* res_str = new char[length+1]; memcpy(res_str, (char*) _str + start, length); res_str[length] = '\0'; return MyString(res_str); } </code></pre> <p>EDIT : If we look at the code you provided (after last edit), it seems that you are actually using an underlying char*. So let's have a look at what you wrote^^</p> <pre><code>MyString sub; sub = new char[length]; </code></pre> <p>What you want to do is actually modify the characters of the underlying char*. So what you should have done is:</p> <pre><code>char* sub; sub = new char[length]; </code></pre> <p>So instead of creating a new MyString, you will create a new char*. You can't directly assign a char* to a MyString (or at least, it's what I think). Now, let's look a the other part of your code:</p> <pre><code>for(int i = start; i &lt;length; i++) { sub[i] = this[i]; } </code></pre> <p>this is a pointer to MyString. So this[i] is equivalent to this.operator which is incalid since this is a pointer. You can't have this followed by a dot. However, if you had written this->operator, it would have searched for a function like char&amp; MyString::operator[](int i). Since you did not defined this function, you would still have a compiler error (it's also the one you currently have for sub[i] since you defined sub as a MyString. You should write:</p> <pre><code>for (int i = start; i &lt; length; i++) { sub[i] = _str[i]; } </code></pre> <p>But it's still provided _str is a char* in your class. Then you will be able to finish your function by:</p> <pre><code>return MyString(sub); </code></pre> <p>But there, it's provided that your MyString class has a constructor that takes a char* as a parameter :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload