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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:03:53+00:00 2026-05-29T04:03:53+00:00

I have a template class ArrayTemp that creates an array of pointers to type

  • 0

I have a template class “ArrayTemp” that creates an array of pointers to type T and am trying to create a derived template that will perform numeric operations. I am having trouble implementing a operator + that will add the elements of each array and return a new array with the sums.

My template base class:

#include <iostream>
using namespace std;

template <class T>
class ArrayTemp
{
private:
    T* arr;
    int arrSize;
    static int DSize;

public:
    ArrayTemp(): arrSize(DSize), arr(new T[DSize]) {}
    ArrayTemp(const ArrayTemp<T>& at): arrSize(at.arrSize), arr(new T[at.arrSize]) 
    {
        for (int i = 0; i < at.arrSize; ++i)
            arr[i] = at.arr[i];
    }
    ~ArrayTemp()
    {
        delete [] arr;
    }

    ArrayTemp<T>& operator = (const ArrayTemp<T>& source)
{
    if (this != &source)
    {
        for (int i = 0; i < arrSize; ++i)
            setElement(source.getElement(i), i);
    }
            return *this;
}

    int getarrSize() { return arrSize; }
    T& getElement(const int index) { return arr[index]; }
    void setElement(const T& x, int index) { arr[index] = x; }
};

My derived template:

#pragma once

#include "ArrayTemp.h"

template <class T>
class numbers: public ArrayTemp<T>
{
public:
    numbers(): ArrayTemp<T>()
    {
        for (int i = 0; i < (getarrSize()); ++i)
            setElement(0, i);
    }
    numbers(const numbers<T>& n): ArrayTemp<T>(const ArrayTemp<T>& x) {}
    ~numbers(){}

    numbers<T> operator + (const numbers<T>& n) const 
    {
        numbers<int> arraysum;

        for (int i = 0; i < getarrSize(); ++i)
            arraysum.setElement((n.getElement(i) + getElement(i)), i);

        return arraysum;
    }
};

template <class T> int ArrayTemp<T>::DSize = 5;

So I can do something like this in main():

numbers<int> n1;
numbers<int> n2;
numbers<int> n3;

n3 = n1 + n2;

How is an overloaded operator + supposed to be implemented within a structure similar to the one I have presented above? Should I try something with a copy constructor returning the numbers object? Also, I presume since there are no new data members in the derived template, that defining an assignment operator that just calls the ArrayTemp assignment operator is sufficient?

Errors I am getting trying this method:

cannot convert 'this' pointer from 'const numbers<T>' to 'ArrayTemp<T> &'
1>          with
1>          [
1>              T=int
1>          ]
1>          Conversion loses qualifiers
while compiling class template member function 'numbers<T> numbers<T>::operator +(const numbers<T> &) const'
1>          with
1>          [
1>              T=int
1>          ]
  • 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-05-29T04:03:54+00:00Added an answer on May 29, 2026 at 4:03 am

    The immediate answer seems to be that you need to declare const-correct versions of your functions in the base template:

    int getarrSize() const { return arrSize; }
    T & getElement(int index) { return arr[index]; }
    T const & getElement(int index) const { return arr[index]; }
    

    A better answer might be that you can probably safe tons of work by using an existing standard container.

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

Sidebar

Related Questions

I have a C++ template class that gets instantiated with 3 different type parameters.
I have a template class that defines a subtype. I'm trying to define the
I have a template class that contains a std::map that stores pointers to T
I am trying to understand templates better I have a template class that starts
I have a template class that I serialize (call it C), for which I
I have a template class that is only valid for couple of template parameters:
I have a template class where I want to use objects of that class
I want to have a template class that looks something like what I have
I have a template class that is: template <class identifier,class registeredObject> class FxPairRegistry :
I am trying to create a template class which contains a static list of

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.