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
CodePudding user response:
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.
CodePudding user response:
#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;
}
}
CodePudding user response:
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
}