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

  • SEARCH
  • Home
  • 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 8967543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:16:01+00:00 2026-06-15T17:16:01+00:00

I’m creating a custom vector class as part of a homework assignment. What I

  • 0

I’m creating a custom vector class as part of a homework assignment. What I am currently trying to do is implement a function called erase, which will take an integer as an argument, decrease my array length by 1, remove the element at the position specified by the argument, and finally shift all the elements down to fill in the gap left by “erased” element.

What I am not completely understanding, due to my lack of experience with this language, is how you can delete a single element from an array of pointers.

Currently, I have the following implemented:

        void myvector::erase(int i)
        {

            if(i != max_size)
            {
                for(int x = i; x < max_size; x++)
                {
                    vec_array[x] = vec_array[x+1];
                }
                vec_size --;

                //delete element from vector;
            }
            else
                //delete element from vector
        }

The class declaration and constructors look like this:

template <typename T> 

class myvector
{
    private:
            T *vec_array;
            int vec_size;
            int max_size;
            bool is_empty;

    public:
            myvector::myvector(int max_size_input)
            {
                max_size = max_size_input;
                vec_array = new T[max_size];
                vec_size = 0;
            }

I have tried the following:

  1. Using delete to try and delete an element

    delete vec_size[max_size];

    vec_size[max_size] = NULL;

  2. Setting the value of the element to NULL or 0

    vec_size[max_size] = NULL

or

vec_size[max_size] = 0

None of which are working for me due to either operator “=” being ambiguous or specified type not being able to be cast to void *.

I’m probably missing something simple, but I just can’t seem to get passed this. Any help would be much appreciated. Again, sorry for the lack of experience if this is something silly.

  • 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-15T17:16:03+00:00Added an answer on June 15, 2026 at 5:16 pm

    If your custom vector class is supposed to work like std::vector, then don’t concern yourself with object destruction. If you need to erase an element, you simply copy all elements following it by one position to the left:

    void myvector::erase(int i)
    {
        for (int x = i + 1; x < vec_size; x++) {
            vec_array[x - 1] = vec_array[x];
        }
        vec_size--;
    }
    

    That’s all the basic work your erase() function has to do.

    If the elements happen to be pointers, you shouldn’t care; the user of your vector class is responsible for deleting those pointers if that’s needed. You cannot determine if they can actually be deleted (the pointers might point to automatic stack variables, which are not deletable.)

    So, do not ever call delete on an element of your vector.

    If your vector class has a clear() function, and you want to make sure the elements are destructed, simply:

    delete[] vec_array;
    vec_array = new T[max_size];
    vec_size = 0;
    

    And this is how std::vector works, actually. (Well, the basic logic of it; of course you can optimize a hell of a lot of stuff in a vector implementation.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.