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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:48:17+00:00 2026-06-05T14:48:17+00:00

I am having a problem understanding what is wrong with the way I set

  • 0

I am having a problem understanding what is wrong with the way I set up my pointers.
I am trying to do something along the following:

matrix[0] = new Array<T>(columnSize);

But it doesn’t compile. I think the problem is that matrix[0] returns a reference, and assigning new Array(columnSize) to it isn’t legal?

Here’s the error

/Users/Ezequiel/desktop/wow/Matrix.h: In constructor ‘Matrix<T>::Matrix(size_t, size_t) [with T = int]’:
main.cpp:8:   instantiated from here
/Users/Ezequiel/desktop/wow/Matrix.h:18: error: invalid conversion from ‘Array<int>*’ to ‘long unsigned int’
/Users/Ezequiel/desktop/wow/Matrix.h:18: error:   initializing argument 1 of ‘Array<T>::Array(size_t) [with T = Array<int>*]’

I attached all of my code below.

// Array.h

#ifndef ARRAY_H
#define ARRAY_H

#include <string>
#include <iostream>
#include <algorithm>

template<class T>
    class Array {
        private:
            T* elements;
            size_t low, high;
            void init(const size_t, const size_t);
            void copy(const Array<T>&);

        public:
            size_t size;
            Array();
            ~Array();
            Array(const size_t);
            Array(const size_t, const size_t);
            Array(const Array<T>&);

            T& operator[](const size_t);
            Array<T>& operator=(const Array<T>&);

            template<class X>
                friend std::ostream& operator<<(std::ostream&, const Array<X>&);
    };

template<class T>
    void Array<T>::init(const size_t low, const size_t high) {
        if (high < low || (high - low + 1 <= 0)) {
            std::cout << "An error was found in the bounds passed to the constructor.\n";
            exit(1);
        }

        size = high - low + 1;
        elements = new T[size];

        this->low = low;
        this->high = high;
    }

template<class T>
    void Array<T>::copy(const Array<T>& someArray) {        
        delete[] elements;

        if (someArray.size) {
            elements = new T[someArray.size];
            std::copy(someArray.elements, someArray.elements + someArray.size, elements);
            low = someArray.low;
            high = someArray.high;
            size = someArray.size;
        } else {
            size = 0;
            elements = 0;
            low = high = -1;    
        }
    }

template<class T>
    Array<T>::Array() {
        size = 0;
        elements = 0;
        low = high = -1;
    }

template<class T>
    Array<T>::Array(const size_t size) {
        init(0, size - 1);
    }

template<class T>
    Array<T>::Array(const size_t low, const size_t high) {
        init(low, high);
    }

template<class T>
    Array<T>::~Array() {
        delete[] elements;
    }

template<class T>
    Array<T>::Array(const Array<T>& someArray) {
        copy(someArray);
    }

template<class T>
    Array<T>& Array<T>::operator=(const Array& someArray) {
        if (this == &someArray) {
            return *this;
        }

        copy(someArray);

        return *this;   
    }

template<class T>
    T& Array<T>::operator[](const size_t index) {
        if (index < low || index > high || size <= 0) {
            std::cout << "The index " << index << " goes out of bounds.\n";
            exit(1);
        }
        return elements[index - low];
    }

template<class X>
    std::ostream& operator<<(std::ostream& os, const Array<X>& someArray) {
        if (someArray.elements) {
            for (size_t i = 0; i < someArray.size - 1; ++i) {
                os << someArray.elements[i] << ", ";
            }
            os << someArray.elements[someArray.size - 1];
        } else {
            os << 0;
        }
    }

#endif

// Matrix.h
#include "Array.h"

template<class T>
    class Matrix {
        private:
            Array< Array<T>* > *matrix;
            size_t rowSize, columnSize;
        public:
            Matrix(const size_t rowSize, const size_t columnSize);
    };

template<class T>
    Matrix<T>::Matrix(const size_t rowSize, const size_t columnSize) {
        matrix = new Array< Array<T>* >(rowSize);

        // Compiles perfectly fine, except after I uncomment this line:
        // matrix[0] = new Array<T>(columnSize);

        this->rowSize = rowSize;
        this->columnSize = columnSize;
    }
  • 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-05T14:48:19+00:00Added an answer on June 5, 2026 at 2:48 pm

    matrix isn’t an Array<Array<T>*>. It’s an Array<Array<T>*>* — a pointer to an Array<Array<T>*>. This means that matrix[0] (better written as *matrix; see below) is of type Array<Array<T>*>.

    You are allocating matrix via new rather than new[]. Only one object is being allocated. You can use matrix[0] to refer to this single object, but since you are allocating with new, it’s better to use the dereference operator. Using matrix[0] might lead you or a reader of your code to think there might be a matrix[1]. There isn’t.

    The way to fix that commented-out statement is to use

    (*matrix)[0] = new Array<T>(columnSize);
    

    and if you are allocating in a loop,

    (*matrix)[idx] = new Array<T>(columnSize);
    

    Note that referencing the i,j element of the matrix is done via (*matrix)[i][j].

    If you insist on using matrix[0] you will need to use

    matrix[0][idx] = new Array<T>(columnSize);
    

    to allocate and matrix[0][i][j] to reference the i,j element of the matrix.

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

Sidebar

Related Questions

I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence
i'm new to c++ and having a little problem understanding about c++'s casting. According
I'm quite new to OpenId and I'm having a bit of a problem understanding
I'm having a problem understanding something about MVVM. My application relies on dialogs for
i having problem understanding what is wrong with my inheritance. I just can not
I'm having a problem understanding what I'm doing wrong here. I have the code
I am half way reading the OWL2 primer and is having problem understanding the
I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following
I'm having a problem understanding how to do something in LINQ. I have a
Hi im pretty new to jsp and servlets so im having a problem understanding

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.