Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:39:49+00:00 2026-06-03T00:39:49+00:00

I am asked to create a Substring() method that returns a substring of the

  • 0

I am asked to create a Substring() method that returns a substring of the original string which begins at location start and is as long as length

Here is how I attempted to implement the function in my .cpp file:

MyString sub;

sub = new char[length];

for(int i = start; i <length; i++)
{
    sub[i] = this[i];
}

return sub;

and I got this error:

error: expected unqualified-id before [ token
MyString.cpp:206: error: no match for operator[] in sub[i]

Note: I am not supposed to overload [].
MyString is the defined class.

What exactly am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:39:50+00:00Added an answer on June 3, 2026 at 12:39 am

    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.

    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.

    With an exemple. If your class is like this

    class MyString
    {
        private:
            std::string _str;
    
        public:
            // Constructor
            MyString(std::string str);
            // Your method
            MyString Substring(int start, int length) const;
    };
    

    Then your Substring method will be like this:

    MyString MyString::Substring(int start, int length) const
    {
        return MyString(_str.substr(start, length));
    }
    

    On the other hand, if your class MyString is like that:

    class MyString
    {
        private:
            char* _str;
    
        public:
            // Constructor
            MyString(char* str);
            // Your method
            MyString Substring(int start, int length) const;
    };
    

    Then your method will be like this:

    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);
    }
    

    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^^

    MyString sub;
    sub = new char[length];
    

    What you want to do is actually modify the characters of the underlying char*. So what you should have done is:

    char* sub;
    sub = new char[length];
    

    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:

    for(int i = start; i <length; i++)
    {
    
            sub[i] = this[i];
    }
    

    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& 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:

    for (int i = start; i < length; i++)
    {
        sub[i] = _str[i];
    }
    

    But it’s still provided _str is a char* in your class. Then you will be able to finish your function by:

    return MyString(sub);
    

    But there, it’s provided that your MyString class has a constructor that takes a char* as a parameter 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm asked to create a method that returns the number of occurrences of a
i have asked to create a module that will record video messages form the
I have been asked to create a vbscript that will set the default font
I’ve been asked to create a CSS (non-HTML5) based site that has a filled
A friend of mine asked to create a static website and I found that
I've been asked to create a stand-alone webapp using straight HTML and Javascript that
I've been asked to create a simple calculations page that will have simple text
I've been asked to create a financial report, which needs to give a total
I was asked to create an MS SQL script that will create a database
I've been asked to create an app that will get data back from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.