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 8643791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:08:19+00:00 2026-06-12T12:08:19+00:00

Firstly I wanted to say this is a HW assignment and I just have

  • 0

Firstly I wanted to say this is a HW assignment and I just have questions in regards to the errors I’m facing

I made a vector template with an insert function that adds a data_type to the end of a dynamic array. This is what I have currently.

// Insert the value at the specified index by moving the tail
// when Size = Capacity the Vector has to be reallocated first 
// to accomate the new element
void Insert(const DATA_TYPE& value, int index){

    // If the Capacity is not large enough then ...
    // allocate large vector
    if (Size >= Capacity){
        // 0. Let's boost the capacity
         Capacity += CAPACITY_BOOST;
        // 1. Allocate new larger vector
         DATA_TYPE* newData = new DATA_TYPE[Capacity];

        // 2. Copy from old Data into the newData
         for( int i=0; i< Size; i++)
                newData[i] = Data[i];

        // 3. Delete the old Data
         delete[] Data;

        // 4. Replace old Data-pointer with the newData pointer
        Data = newData;
    }

    // Move the tail
    for(int i=index; i<Size;i++){
        Data[i+1] = Data[i];
    }

    // Insert
    Data[index] = value;
    Size++;

}
DATA_TYPE& operator[] (int index) const{
    return *this[index];
}

Note: Using Private Variables: Size, Capacity, Data(stores the dynamic array)
I’m pretty sure I’ve implemented the add or push_back function correctly. The problem is when ever I try to cout something like “cout << a[1]; ” I get an error.

while compiling class template member function 'int &Vector<DATA_TYPE>::operator [](int) const'
      with
      [
          DATA_TYPE=int
      ]
see reference to class template instantiation 'Vector<DATA_TYPE>' being compiled
     with
     [
          DATA_TYPE=int
      ]
error C2440: 'return' : cannot convert from 'const Vector<DATA_TYPE>' to 'int &'
    with
     [
        DATA_TYPE=int
      ]
  • 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-12T12:08:21+00:00Added an answer on June 12, 2026 at 12:08 pm

    There should be 2 versions of your operator[]:

    const DATA_TYPE& operator[] (int index) const;
    DATA_TYPE& operator[] (int index);
    

    What you have is a weird combination of the two.

    You should also be returning

    return Data[index];
    

    Returning (*this)[index]; would result in an infinite recursive call (well, not infinite, you’ll get a stackoverflow before that).

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

Sidebar

Related Questions

I have wanted to do this a few times, for various reasons. Firstly in
Firstly, this is a homework assignment, and I am very new to programming in
Firstly, let me say that I've been on this issue for pretty much 2
Firstly apologies if this is really simple but I have spent hours trying and
I Have created one UnitTesting Project in C# and facing some issues. Firstly, I
Firstly apologises for the length of code but I wanted to show it all.
Firstly, I wrote my customized setter for an NSString* like this: - (void)setDateString:(NSString *)newDateString
Firstly sorry if this is a common question but I couldn't find anything on
Firstly, I know this [type of] question is frequently asked, so let me preface
I have a table in SQL Server which looks like this: ID Code Name

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.