Implementing ArrayList in C++

I'm trying to implement my own ArrayList in C++, and I can't get past syntax errors in order to complete the program. Here is what I have so far, and it is underlining just about every variable name, saying it's not in the proper scope, etc. Help?


#include "ArrayList.h";
#include <iostream>;
using namespace std;

ArrayList :: ArrayList()
{
int elements = 0;
int lastIndex=0;
int *arrayList= new int [elements];
}

void ArrayList :: add(int n, )
{

arrayList&[lastIndex]=n;



}

ArrayList* doubleSize(int [] arr&)
{
int [] arr2;= new ArrayList[arr->length*2];
for(int i=0; i<arr.length; i++)
arr2[i]=arr[i];
return arr2;
}

int ArrayList :: deleteLast()
{
int numDeleted=0;
if(arrayList!=0)
{
numDeleted=arrayList[size];
arrayList[size]=NULL;
if(size<
}
else
{
cout << "Array is empty";
}

return numDeleted;
}

int size()
{
int numElems=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=NULL)
numElems++;
}

return numElems;


}

int main()
{
cout << "Test" << endl;
}


Either your copy and paste didn't work or you didn't type everything correctly because there are so many syntax errors in this post.

int [] arr2;= new ArrayList[arr->length*2];

if(size<

If ArrayList.h has errors like these no wonder everything is showing up as an error. Just start commenting chunks of code and fix each syntax error 1 by 1, uncommenting new sections as you progress. Eventually you will have it fixed or narrowed down so we can help you better.
1
2
3
int elements = 0;
int lastIndex=0;
int *arrayList= new int [elements];
Here you are declaring new variables that will only exists inside the constructor. If ArrayList has the member variables elements, lastIndex and arrayList just do normal assignment or use the constructor initialization list.

arrayList&[lastIndex]=n; The & should not be there and shouldn't you increase elements and/or lastIndex in that function?

int [] arr&What is this? An int array? In that case it should be int arr[]. arr is actually a pointer to an int so int* arr will do the same thing.

int [] arr2;= new ArrayList[arr->length*2]; This has 3 problems.
1. When you have dynamically allocated raw arrays in C++ you have a pointer to the first element in the array. Replace [] with * to make arr2 a pointer.
2. That semicolon after arr2 shouldn't be there.
3. arr is just a pointer so there is no way to know the length of the array pointed to. What you can do is pass the length as an extra argument to the function.

If size is a member function of ArrayList you need to write int ArrayList::size() when you define the function.

It's obvious that you are used to program in Java ;) Note that the equivalent of ArrayList in C++ is std::vector.
Last edited on
How do I make it so that I can access these variables throughout all the functions in my ArrayList.cpp? For instance I want to say

arrayList[lastIndex]=n;
elements++;
lastIndex++;

and yet it underlines everything saying that they are undefined.

Also, I am still getting an error whenever I refer to the ArrayList class:

ArrayList :: ArrayList()

void ArrayList :: add(int n)


etc.
Have you defined the class in the header and declared all the functions it should have?

1
2
3
4
5
6
7
8
9
10
11
class ArrayList
{
public:
	ArrayList();
	int size();
	...
private:
	int elements;
	int lastIndex;
	...
};
Here is my header:


#if ndef _ARRAYLIST_H_
#define _ARRAYLIST_H_
#include <iostream>

class ArrayList
{
private:
int elements;
int lastIndex;
int *arrayList;
ArrayList doubleSize(int* arr, int length)
ArrayList halfSize(int* arr, int length)

public:
ArrayList();
void add(int n);
int deleteLast();
int size();
~ArrayList();

};
#endif
Did you put ArrayList:: in front of all the function names when you define the functions, as I told you to do with the size member function?
Last edited on
Yes, but to no avail. I am still getting errors in my .cpp.
Here are my files [updated]:

.cpp
#include "ArrayList.h"
#include <iostream>
using namespace std;

ArrayList :: ArrayList()
{
int elements = 0;
int lastIndex=0;
int *arrayList= new int [elements];
}

void ArrayList :: add(int n)
{

arrayList[lastIndex]=n;
elements++;
lastIndex++;

if(elements==lastIndex)
{
arrayList=doubleSize(arrayList, elements);
}

}

ArrayList :: doubleSize(int* arr, int length)
{
int *arr2= new int[length*2];
for(int i=0; i<arr.length; i++)
arr2[i]=arr[i];
return arr2;
}

int ArrayList :: deleteLast()
{
int numDeleted=0;
if(arrayList!=0)
{
numDeleted=arrayList[size];
arrayList[size]=NULL;
if(elements<lastIndex*2)
{
arrayList=halfSize(arrayList, lastIndex);
}


}
else
{
cout << "Array is empty";
}

return numDeleted;
}


ArrayList halfSize(int* arr, int length)
{
int *arr2= new int[length/2];
for(int i=0; i<arr.length; i++)
arr2[i]=arr[i];
return arr2;

}

int size()
{
int numElems=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=NULL)
numElems++;
}

return numElems;


}

int main()
{
cout << "Test" << endl;
}

and



.h

#if ndef _ARRAYLIST_H_
#define _ARRAYLIST_H_
#include <iostream>

class ArrayList
{
private:
int elements;
int lastIndex;
int *arrayList;
ArrayList doubleSize(int* arr, int length)
ArrayList halfSize(int* arr, int length)

public:
ArrayList();
ArrayList::void add(int n);
ArrayList::int deleteLast();
ArrayList::int size();
ArrayList::~ArrayList();

};
#endif

So, you're trying to recreate ArrayList from Java I take it? Why not just use vector?
I'm going to implement methods that vector does not offer, also due to program requirements I cannot.
Your header..
.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#if ndef _ARRAYLIST_H_
#define _ARRAYLIST_H_
#include <iostream>

class ArrayList
{
private:
int elements;
int lastIndex;
int *arrayList;
ArrayList doubleSize(int* arr, int length)
ArrayList halfSize(int* arr, int length)

public:
ArrayList();
ArrayList::void add(int n); //Remove ArrayList::
ArrayList::int deleteLast();//Remove ArrayList::
ArrayList::int size();          //Remove ArrayList::
ArrayList::~ArrayList();     //Remove ArrayList::

};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "ArrayList.h"
#include <iostream>
using namespace std;

ArrayList :: ArrayList()
{
int elements = 0;  //Error redefinition of elements, should be "elements = 0;" better yet use the initializer list.
int lastIndex=0;   //Error same as above
int *arrayList= new int [elements];  //Error same as above
}

void ArrayList :: add(int n)
{

arrayList[lastIndex]=n;
elements++;
lastIndex++;

if(elements==lastIndex)
{
arrayList=doubleSize(arrayList, elements);
}

}

ArrayList :: doubleSize(int* arr, int length)
{
int *arr2= new int[length*2];
for(int i=0; i<arr.length; i++)
arr2[i]=arr[i];
return arr2;
}

int ArrayList :: deleteLast()
{
int numDeleted=0;
if(arrayList!=0)
{
numDeleted=arrayList[size];
arrayList[size]=NULL;
if(elements<lastIndex*2)
{
arrayList=halfSize(arrayList, lastIndex);
}


}
else
{
cout << "Array is empty";
}

return numDeleted;
}


ArrayList halfSize(int* arr, int length) //Missing ArrayList::
{
int *arr2= new int[length/2];
for(int i=0; i<arr.length; i++)
arr2[i]=arr[i];
return arr2;

}

int size() //Missing ArrayList::
{
int numElems=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=NULL)
numElems++;
}

return numElems;


}

int main()
{
cout << "Test" << endl;
}


There are still other bugs but this will get you much closer.
Topic archived. No new replies allowed.