Types and Templates
Feb 29, 2016 at 9:30am UTC
Help I dont know what Im doing wrong, so please tell me because I have this.
ArraList.h
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
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
#define DEFAULT_MAX_SIZE 1024
#include <stdexcept>
using namespace std;
template <typename E>
class ArrayList
{
private :
E *elements;
int max = DEFAULT_MAX_SIZE;
int size;
int pos;
public :
ArrayList(int pMax = DEFAULT_MAX_SIZE);
virtual ~ArrayList();
void insert(E pElement)throw (runtime_error);
void append(E pElement)throw (runtime_error);
E remove()throw (runtime_error);
void clear();
E getElement() throw (runtime_error);
void goToStart();
void goToEnd();
void goToPos(int pPos)throw (runtime_error);
void previous();
void next();
int getPos(){return pos;};
int getSize(){return size;}
void printList();
};
class template ArrayList<int >;
class template ArrayList<float >;
class template ArrayList<double >;
#endif // ARRAYLIST_H
ArrayList.cpp
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#include "ArrayList.h"
#include <iostream>
using namespace std;
template <typename E>
ArrayList<E>::ArrayList(int pMax){
elements = new E[pMax];
max = pMax;
size = 0;
pos = 0;
}
template <typename E>
ArrayList<E>::~ArrayList()
{
delete [] elements;
}
template <typename E>
void ArrayList<E>::insert(E pElement) throw (runtime_error){
if (size == max){
throw runtime_error("List is full" );
}
for (int i = size; i > pos; i--){
elements[i] = elements[i-1];
}
elements[pos] = pElement;
size++;
}
template <typename E>
void ArrayList<E>::append(E pElement) throw (runtime_error){
if (size == max){
throw runtime_error("List is full" );
}
elements[size] = pElement;
size++;
}
template <typename E>
E ArrayList<E>::remove() throw (runtime_error){
if (size == 0){
throw runtime_error("List is empty" );
}
if ((pos < 0) || (pos >= size)){
throw runtime_error("Index out of bounds" );
}
E temp = elements[pos];
for (int i = pos; i < size-1; i++){
elements[i] = elements[i+1];
}
size--;
return temp;
}
template <typename E>
void ArrayList<E>::clear(){
size = 0;
pos = 0;
delete [] elements;
elements = new E[max];
}
template <typename E>
E ArrayList<E>::getElement() throw (runtime_error){
if (size == 0){
throw runtime_error("List is empty" );
}
if (pos > size){
throw runtime_error("No element" );
}
return elements[pos];
}
template <typename E>
void ArrayList<E>::goToStart(){
pos = 0;
}
template <typename E>
void ArrayList<E>::goToEnd(){
pos = size;
}
template <typename E>
void ArrayList<E>::goToPos(int pPos) throw (runtime_error){
if ((pPos < 0) || (pPos > size)){
throw runtime_error("Index out of bounds" );
}
pos = pPos;
}
template <typename E>
void ArrayList<E>::previous(){
if (pos > 0){
pos--;
}
}
template <typename E>
void ArrayList<E>::next(){
if (pos < size){
pos++;
}
}
template <typename E>
void ArrayList<E>::printList(){
goToStart();
for (int j = 0; j < getSize(); j++){
cout<<getElement()<<" " <<endl;
next();
}
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include "ArrayList.h"
using namespace std;
int main()
{
ArrayList<int > AL(10);//Error Here: error: variable 'ArrayList<int> AL' has initializer but incomplete type|
for (int i=0;i<22;i++){
AL.insert(i);
}
AL.printList();
return 0;
}
I have been fighting so hard with those typenames because I dont usually use them. And now I dont know whats happening but I kinda guess that fixing must be so idiotic and I hate for not knowing the answer to it.
Feb 29, 2016 at 10:30am UTC
Hi,
All of the templates must be in a header file.
Why do you have this ?
1 2 3
class template ArrayList<int >;
class template ArrayList<float >;
class template ArrayList<double >;
I was able to get it to compile without that.
A couple of other things:
I haven't seen
throw
used like this before:
1 2
template <typename E>
E ArrayList<E>::remove() throw (runtime_error){
Feb 29, 2016 at 1:56pm UTC
> Why do you have this ?
An
explicit instantiation definition (while template code is under development) helps catch errors early.
Syntax for explicit instantiation definition of the class template :
template class ArrayList<int > ;
Ideally, place this outside the header, and also have an explicit instantiation definition for a non-trivial type.
For instance
template class ArrayList<std::string> ;
> I haven't seen throw used like this before:
Dynamic exception specification. Deprecated in C++11 and may be removed in C++17.
http://en.cppreference.com/w/cpp/language/except_spec
Feb 29, 2016 at 2:11pm UTC
@JLBorges
Thanks, awesome as per usual :+)
Topic archived. No new replies allowed.