error: expected primary-expression before '[' token

Nov 13, 2019 at 9:10am
I am not sure what is it that they want me to type there before '['
I get this error message:
1
2
3
4
C:\Dev-Cpp\Chapter2\listType.cpp: In member function `void listType<elemType>::printList()':
C:\Dev-Cpp\Chapter2\listType.cpp:86: error: expected primary-expression before '[' token

Execution terminated 


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

template <class elemType>
class listType
{
public:
bool isEmpty();
bool isFull();
void search(const elemType& searchItem, bool& found);
void insert(const elemType& newElement);
void remove(const elemType& removeElement);
void destroyList();
void printList();
listType();
private:
elemType list[100]; //array to hold the list elements
int length; //variable to store the number
//of elements in the list
};
template<class elemType>
void listType<elemType>::printList()
{
     for(int i = 0; i < length; i++)
        cout << listType[i]<<" " ;
        
        cout <<endl;
}
Nov 13, 2019 at 9:15am
Did you mean
cout << list[i]<<" " ;
rather than
cout << listType[i]<<" " ;
Nov 13, 2019 at 9:50am
Thank you lastchance I have corrected that, compilation went fine but now my list does not print
1
2
3
4
5
6
C:\Dev-Cpp\Chapter2>listType
Enter numbrs ending with -999
56 78 98 23 -999


C:\Dev-Cpp\Chapter2>
Nov 13, 2019 at 9:52am
Try cout'ing the variable length.

Just to make sure that something actually got into the list ...
Nov 13, 2019 at 10:49am
There is nothing in the list:
1
2
3
4
5
6
7
8

C:\Dev-Cpp\Chapter2>listType
Enter numbrs ending with -999
56 1 34 677 1732 -999
The list contains: 0

C:\Dev-Cpp\Chapter2>
Nov 13, 2019 at 11:02am
So the problem obviously isn't in the snippet of code that you have given.
Nov 14, 2019 at 10:16am
The problem was in my insert function. Its all sorted out now.
here is the correct output:
1
2
3
4
5
6
C:\Dev-Cpp\Chapter2>listType
Enter numbrs ending with -999
45 67 89 12 34 -999
45 67 89 12 34

C:\Dev-Cpp\Chapter2>
Topic archived. No new replies allowed.