Program compiles OK! but bombs out when I run it

.Cannot work out what is the problem here:

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
  #include<iostream>
#include<cassert>
using namespace std;

template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item) const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem) const;
void replaceAt(int location, const elemType& repItem);
void clearList();
int seqSearch(const elemType& item) const;
void insert(const elemType& insertItem);
void remove(const elemType& removeItem);
arrayListType(int size = 100);
arrayListType(const arrayListType<elemType>& otherList);
//~arrayListType();
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
//template<class int>
arrayListType<int>::arrayListType(int size)
{
   size = 100;
}                              
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
} //end seqSearch

template<class elemType>
void arrayListType<elemType>::insert(const elemType &insertItem)
{
     int loc;
     
     if(length == 0)    //the list is empty
        list[length++] = insertItem;  //insert the item and
                                      //increment the length
     else
        if(length == maxSize)
           cerr<<"Cannot insert in a full list,"<<endl;
        else
        {
            loc = seqSearch(insertItem);
            
            if(loc == -1)    //the item to be inserted
                                //does not exist in the list
             list[length++] = insertItem;
            else
               cerr<<"The item to be inseted is already in "
                   <<"the list. No duplicates are allowed."<<endl;
                   }
}// end insert
template<class elemType>
void arrayListType<elemType>::print() const
{
     for (int i = 0; i < length; i++)
         cout << list[i] << " ";
     cout << endl;
}                   
int main()
{
    arrayListType<int> myList;
    myList.insert(56);
    myList.insert(111);
    myList.insert(54);
    myList.insert(1732);
    myList.print();
}    
Does not compile:
35:1: error: specializing member 'arrayListType<int>::arrayListType' requires 'template<>' syntax
I have included the template<> syntax but I still get the same error:
1
2
3
4
5
6

template<class elemType>
arrayListType<elemType>::arrayListType(int size)
{
   size = 100;
}    
Apart from the compiler error @keskiverto pointed out: You never allocate memory for list. I guess you want something like this (line 35):
1
2
3
4
5
template <class elemType>
arrayListType<elemType>::arrayListType(int size) : maxSize{size}, length{0}, list{new elemType[size]{}} // Note: initializer list is a good place to initialize your member variables!
{
   size = 100;
} 
It works!!!! could not believe it......
here is what I have edited:
1
2
3
4
5
6
7
8

template<class elemType>
arrayListType<elemType>::arrayListType(int size)
{
   maxSize = size;
   length = 0;
   list = new elemType[size]; 
}   

Here is the output:
1
2
3
4
5

C:\Dev-Cpp\Chapter9>seqSearch
56 111 54 1732

C:\Dev-Cpp\Chapter9>

Thank you all!!!
now I can go on and deal with the seqSearch section
Registered users can post here. Sign in or register to post.