1

I was trying yo create my own array class (similar to std::vector) just for fun but there is some problem... The Array class code itself works and compiles successfully but throws an error if i try to instantiate an object of Array class.

#include<iostream>

template<typename type, int size>
class Array
{
 private:
 type _mArray[size] = new type[size];
 public:
 int Access(int index)
 {
  return _mArray[index];
 }
 int Len()
 {
   return size;
 }
 void Insert(int index, type val)
 {
  _mArray[index] = val;
 }
 ~Array()
 {
  delete[] _mArray; 
 }
};//All code above compiles successfully

int main()
{
  Array<int, 2> name; //this line throws an error
}

I am a bit new to C++ so if someone can explain then I will be very thankful....

Btw here is the error Array initializer must be an initializer list

3
  • If you want to have a dynamic array like std::vector, don't make size a template parameter. Also type of size should not be an int but std::size_t (avoids having to check for index < 0). Check the input indices against the size of the actual array (or you can get out of bound access). Commented Dec 27, 2021 at 7:11
  • The Array class code itself works -- int main() { Array<int, 2> name; Array<int, 2> name2; name = name2; } -- Double deletion error at runtime. You still have work today to get this code to actually be useful. Commented Dec 27, 2021 at 7:18
  • Look more like std::array (fixed size array) than std::vector (dynamic size array). Commented Dec 27, 2021 at 10:30

3 Answers 3

2
type _mArray[size] = new type[size];

The template instantiates with: type is int, and size is 2. Therefore, this becomes:

int _mArray[2] = new int[2];

This obviously does not make much sense. If you put this, verbatim, in your main() your C++ compiler will also serve you with the same complaint.

It's clear that the intent here is, simply:

type _mArray[size];

And nothing else.

P.S. Now, let's go back and reread what the suffering C++ compiler was struggling to communicate here:

Array initializer must be an initializer list

int _mArray[2] is, obviously, an array. There's an = stuck after it. Ok, this must be array initialization. How do you initialize an array in C++? With a braced initialization list, of course. This would be something like this, for example:

int _mArray[2]={1, 2};

The C++ compiler saw nothing of that kind, and was trying to tell you that.

Sign up to request clarification or add additional context in comments.

2 Comments

Very good explanation +1
destructor has to be fixed/removed then :-)
0
#include<iostream>

template<typename type, int size>
class Array
{
 private:
 type * _mArray ;
 public:
 int Access(int index)
 {
  return _mArray[index];
 }
 int Len()
 {
   return size;
 }

 Array()
 {
   _mArray = new type[size];
 }
 ~Array()
 {
  delete[] _mArray; 
 }
 int& operator[](int index){
    return _mArray[index];
 }
};//All code above compiles successfully

int main()
{
  Array<int, 2> name; 
  name[0] = 1024;
  name[1] = 100;

  for(int i= 0; i< name.Len(); i++)
  {
     std::cout<< name[i] << std::endl;
  }

}

2 Comments

And what? Just code dump?
0

You can get it to build using the following minimal change:

@@ -4,7 +4,7 @@ template<typename type, int size>
 class Array
 {
  private:
- type _mArray[size] = new type[size];
+ type* _mArray;
  public:
  int Access(int index)
  {
@@ -18,6 +18,10 @@ class Array
  {
   _mArray[index] = val;
  }
+ Array()
+ {
+  _mArray = new type[size]; 
+ }
  ~Array()
  {
   delete[] _mArray; 

Basically, you should be initializing the array in your constructor, and store a pointer to it as a class member. The following code builds:

#include<iostream>

template<typename type, int size>
class Array
{
 private:
 type* _mArray;
 public:
 int Access(int index)
 {
  return _mArray[index];
 }
 int Len()
 {
   return size;
 }
 void Insert(int index, type val)
 {
  _mArray[index] = val;
 }
 Array()
 {
  _mArray = new type[size]; 
 }
 ~Array()
 {
  delete[] _mArray; 
 }
};//All code above compiles successfully

int main()
{
  Array<int, 2> name; //this line throws an error
}

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.