edited my program, still have 3 small errors i can't find

program2.cpp: In member function `void container::allocate()':
program2.cpp:125: error: invalid types `int*[int*]' for array subscript
program2.cpp: In member function `void container::resize(int)':
program2.cpp:181: error: expected primary-expression before ']' token
program2.cpp: In function `int main()':
program2.cpp:224: error: `resize' undeclared (first use this function)
program2.cpp:224: error: (Each undeclared identifier is reported only once for each function it appears in.)

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  // Container with dynamic storage
#include <iostream>
#include <iomanip>
using namespace std; 

class container {
	friend ostream& operator<<(ostream& out, container &);
	// Postcondition: displays # of values stored in the container, storage capacity of the contianer, and stored values in the container 
	//                in the following format: Array size = 3, capacity = 4, contents = 11, 22, 33 (see below sample program output

public:
	container();
	// Postcondition: set dynamic storage capacity to 1 and count to -1 where (count + 1) represents the actual values stored 
	//                 in the container. Notice that data member count is used as the subscript to access elements (actual values) stored 
	//                 in the dynamic array; thus (count + 1) represents the total # of values that are currently stored in the array
	
	container(int n);
	// Postcondition: set dynamic storage (data array) capacity to n and count to -1
	
	container(container &obj);
	// Programmer-supplied copy constructor is necessary to avoid memory leak and other side effect
	// Postcondition: a new container class object is created which is the same as the one passed to the function
	
	~container();
	// Programmer-supplied destructor is necessary to avoid memory leak
	// Postcondition: all dynamic memory locations have been returned back to the heap whenever a container object goes out of scope
	
	container& operator=(const container &rhs);
	// Programmer-supplied overloaded assignment is necessary to avoid memory leak and other side effect
	// Postconditoin: the container object rhs is assigned to the calling object
   
	void insert(int value);
	// Postcondition: if the container is not full, the value passed to the function is stored in  
	//			the first available element of the dynamic array. Otherwise the function calls the private 
	//         	       "allocate" member function requesting a new set of dynamic memory with twice the previous storage capacity 
	//			the insert function then increments count by 1 and insert the value into the new and larger array.
	
	void remove();
	//  Precondition: the data array must not be empty; i.e., count must be greater than or equal to 0.
	// Postcondition: if the container is not empty, then remove the most recently stored value ifrom the container and 
	//			decrement count by 1; otherwise, display the message "The container is empty; no action is taken!"
	
	int operator[](int sub);
	//  Precondition: value passed to the function must be a positive integer including 0
	// Postcondition: the value of stored in data[sub] is returned; if sub is out of range, display a message and terminate the program .

	bool isFull();
	// Postcondition: return true if the container is full; return false otherwise 

	bool isEmpty();
	// Postcondition: return true if the container is empty; return false otherwise

	int Capacity();
	// Notice uppercase 'C' to avoid conflict with data member named "capacity"
	// Postcondition: returns the current storage capacity of the container
	
	int size();
	// Postcondition: returns the # of elements (# of objects) currently stored in the container

	void resize(int n);
	// Postcondition: container (i.e., the dynamic array) is resized to n; contents of existing container have been copied to the new array; 
	// 			      old array is deleted to avoid memory leak.

private:
	void allocate();
	// Postcondition: 1) the capacity of the container has been doubled, 2) existing values in the existing array have been copied to 
	//				   the new and larger dynamic array, 3) memory of the old array has been deleted (returned to "heap").
			  
	int *data;	
	int capacity;		// indicates the storage capcity of the container, i.e., the size of the dynamic array		
   	int count;		// used as a subscript to index into the array; size = count + 1
};

container::container()
{
	capacity =1;
	data = new int[capacity];
	count = -1;
}
container::container(int n)
{
	capacity=n;
	data=new int[capacity];
	count =-1;
}
container::container(container & obj)
{
	data=new int[capacity];
	for(int i=0;i<capacity;i++)
	data[i]=obj.data[i];
	count = obj.count;
}
container::~container()
{
	delete[]data;
}
container & container::operator=(const container &rhs)
{
	if(this!=&rhs)
	{
		if(capacity!=rhs.capacity)
		{
			delete[] data;
			capacity = rhs.capacity;
			data=new int [capacity];
		for(int i=0;i<rhs.count;i++)
			data[i]=rhs.data[i];
		count=rhs.count;
		}
		else
		{
			data=rhs.data;
			count=rhs.count;
			for(int i=0;i<rhs.count;i++)
			data[i]=rhs.data[i];
		}
	}
	return *this;
}
void container::allocate()
{
	int *newcapacity;
	newcapacity=new int(capacity*2);
	for(int i=0;i<capacity;i++)
	data[newcapacity]=data[capacity];
	delete[] data;
}
void container::insert(int value)
{
	if(isFull())
	{
		void allocate();
		count++;
		data[count]=value;
	}
	count++;
	data[count]=value;
}
void container::remove()	// logical removal
{
	if( isEmpty( ) )		// the stack is empty
	{
		cout << endl << "Stack is empty" << endl;
		exit( 1 );
	}
	--count;
}
int container::operator[](int sub)
{
	if(sub<0||sub==capacity)
	{
	
		cout<<"sub is out of range";
	exit(1);
	}
	return data[sub];
}
bool container::isEmpty()
{ 
	return( count == -1 );
}

bool container::isFull()
{ 
	return( count == capacity -1 );
}
int container::Capacity()
{
	return capacity;
}
int container::size()
{
	return count;
}
void container::resize(int n)
{
	
	int *newdata = new int[n];
	for(int i=0;i<capacity;i++)
	newdata[i]=data[i];
	delete data [];


}
ostream& operator<<(ostream& out, container & obj)
{
	out << "Array size = "<<obj.size();
	out<< "capacity = "<<obj.Capacity();
	out<<"contents = ";
	if (obj.count == -1)
		out << "*** data array is empty!" << endl;
	else
	{
		for (int i = 0; i <= obj.count; i++)
			out << obj.data[i] << '\t';
		out << endl;
	}
return out;
}

int main()
{
	container c1;
	cout<<"Before inserting any value into the c1 container"<<endl;
	cout<<c1;
	
	cout<<"We now insert 11 to 99 with incremental value of 11 into c1 container."<<endl;
	cout<<"container c1:"<<endl;
	for (int i = 11; i < 99; i=i+11)
		c1.insert(i);
	cout<<c1;
	
	container c2(c1);
	cout<<"Declared c2 as a copy of c1 using the copy constructor."<<endl;
	cout<<"container c2:"<<endl;
	cout<<c2;

	container c3;
	c3=c2;
	cout<<"Declared c3 and assign c2 to c3 with overloaded assignment operator."<<endl;
	cout<<"container c3:"<<endl;
	cout<<c3;
	
	c3=resize(4);
	cout<<"Resized c3 to size = 4."<<endl;
	cout<<"container c3:"<<endl;
	cout<<c3;

	c3=resize(7);
	cout<<"Resized c3 to size = 7."<<endl;
	cout<<"container c3:"<<endl;
	cout<<c3;

	cout<<"Finally, we display the address of three container:"<<endl;
	cout<<"Address of c1 = &c1 = "<<&c1<<endl;
	cout<<"Address of c2 = &c2 = "<<&c2<<endl;
	cout<<"Address of c3 = &c3 = "<<&c3<<endl;
	return 0;
}

1
2
c3=resize(4) // not sure what you're expecting out of this;
c3=resize(7);

I think you mean c3.resize(4); and c3.resize(7);

line 181: delete data []; should be delete[] data;
Last edited on
@tarikneaj YES that is what I meant, not sure why I wrote it that way lol, thanks! but now i just get this error

program2.cpp: In member function `void container::allocate()':
program2.cpp:125: error: invalid types `int*[int*]' for array subscript

Last edited on
You do realize "newcapacity" is a pointer right? How can you use a pointer to determine the size of the array?
You're right it doesn't make sense, I was trying to copy the data inside capacity into the new size capacity, was I not supposed to make it a pointer, Im just not sure how to copy the old elements into the new size capacity. Do I need a temp variable to move it in then copy it in the new size capacity ?
still having trouble with this, can someone please help =[
Wht is the problem,pls specify or we cant help you.
trying to copy the data inside capacity into the new size capacity, not sure how to copy the old elements into the new size capacity. Do I need a temp variable to move it in then copy it in the new size capacity ?
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Container with dynamic storage
#include <iostream>
#include <iomanip>
using namespace std; 

class container {
	friend ostream& operator<<(ostream& out, container &);
	// Postcondition: displays # of values stored in the container, storage capacity of the contianer, and stored values in the container 
	//                in the following format: Array size = 3, capacity = 4, contents = 11, 22, 33 (see below sample program output

public:
	container();
	// Postcondition: set dynamic storage capacity to 1 and count to -1 where (count + 1) represents the actual values stored 
	//                 in the container. Notice that data member count is used as the subscript to access elements (actual values) stored 
	//                 in the dynamic array; thus (count + 1) represents the total # of values that are currently stored in the array
	
	container(int n);
	// Postcondition: set dynamic storage (data array) capacity to n and count to -1
	
	container(container &obj);
	// Programmer-supplied copy constructor is necessary to avoid memory leak and other side effect
	// Postcondition: a new container class object is created which is the same as the one passed to the function
	
	~container();
	// Programmer-supplied destructor is necessary to avoid memory leak
	// Postcondition: all dynamic memory locations have been returned back to the heap whenever a container object goes out of scope
	
	container& operator=(const container &rhs);
	// Programmer-supplied overloaded assignment is necessary to avoid memory leak and other side effect
	// Postconditoin: the container object rhs is assigned to the calling object
   
	void insert(int value);
	// Postcondition: if the container is not full, the value passed to the function is stored in  
	//			the first available element of the dynamic array. Otherwise the function calls the private 
	//         	       "allocate" member function requesting a new set of dynamic memory with twice the previous storage capacity 
	//			the insert function then increments count by 1 and insert the value into the new and larger array.
	
	void remove();
	//  Precondition: the data array must not be empty; i.e., count must be greater than or equal to 0.
	// Postcondition: if the container is not empty, then remove the most recently stored value ifrom the container and 
	//			decrement count by 1; otherwise, display the message "The container is empty; no action is taken!"
	
	int operator[](int sub);
	//  Precondition: value passed to the function must be a positive integer including 0
	// Postcondition: the value of stored in data[sub] is returned; if sub is out of range, display a message and terminate the program .

	bool isFull();
	// Postcondition: return true if the container is full; return false otherwise 

	bool isEmpty();
	// Postcondition: return true if the container is empty; return false otherwise

	int Capacity();
	// Notice uppercase 'C' to avoid conflict with data member named "capacity"
	// Postcondition: returns the current storage capacity of the container
	
	int size();
	// Postcondition: returns the # of elements (# of objects) currently stored in the container

	void resize(int n);
	// Postcondition: container (i.e., the dynamic array) is resized to n; contents of existing container have been copied to the new array; 
	// 			      old array is deleted to avoid memory leak.

private:
	void allocate();
	// Postcondition: 1) the capacity of the container has been doubled, 2) existing values in the existing array have been copied to 
	//				   the new and larger dynamic array, 3) memory of the old array has been deleted (returned to "heap").
			  
	int *data;	
	int capacity;		// indicates the storage capcity of the container, i.e., the size of the dynamic array		
   	int count;		// used as a subscript to index into the array; size = count + 1
};

container::container()
{
	capacity =1;
	data = new int[capacity];
	count = -1;
}
container::container(int n)
{
	capacity=n;
	data=new int[capacity];
	count =-1;
}
container::container(container & obj)
{
	data=new int[capacity];
	for(int i=0;i<capacity;i++)
	data[i]=obj.data[i];
	count = obj.count;
}
container::~container()
{
	delete[]data;
}
container & container::operator=(const container &rhs)
{
	if(this!=&rhs)
	{
		if(capacity!=rhs.capacity)
		{
			delete[] data;
			capacity = rhs.capacity;
			data=new int [capacity];
		for(int i=0;i<rhs.count;i++)
			data[i]=rhs.data[i];
		count=rhs.count;
		}
		else
		{
			data=rhs.data;
			count=rhs.count;
			for(int i=0;i<rhs.count;i++)
			data[i]=rhs.data[i];
		}
	}
	return *this;
}
void container::allocate()
{
	int *newcapacity;
	newcapacity=new int(capacity*2);
	for(int i=0;i<capacity;i++)
	data[newcapacity]=data[capacity];
	delete[] data;
}
void container::insert(int value)
{
	if(isFull())
	{
		void allocate();
		count++;
		data[count]=value;
	}
	count++;
	data[count]=value;
}
void container::remove()	// logical removal
{
	if( isEmpty( ) )		// the stack is empty
	{
		cout << endl << "Stack is empty" << endl;
		exit( 1 );
	}
	--count;
}
int container::operator[](int sub)
{
	if(sub<0||sub==capacity)
	{
	
		cout<<"sub is out of range";
	exit(1);
	}
	return data[sub];
}
bool container::isEmpty()
{ 
	return( count == -1 );
}

bool container::isFull()
{ 
	return( count == capacity -1 );
}
int container::Capacity()
{
	return capacity;
}
int container::size()
{
	return count;
}
void container::resize(int n)
{
	
	int *newdata = new int[n];
	for(int i=0;i<capacity;i++)
	newdata[i]=data[i];
	delete[] data;


}
ostream& operator<<(ostream& out, container & obj)
{
	out << "Array size = "<<obj.size();
	out<< "capacity = "<<obj.Capacity();
	out<<"contents = ";
	if (obj.count == -1)
		out << "*** data array is empty!" << endl;
	else
	{
		for (int i = 0; i <= obj.count; i++)
			out << obj.data[i] << '\t';
		out << endl;
	}
return out;
}

int main()
{
	container c1;
	cout<<"Before inserting any value into the c1 container"<<endl;
	cout<<c1;
	
	cout<<"We now insert 11 to 99 with incremental value of 11 into c1 container."<<endl;
	cout<<"container c1:"<<endl;
	for (int i = 11; i < 99; i=i+11)
		c1.insert(i);
	cout<<c1;
	
	container c2(c1);
	cout<<"Declared c2 as a copy of c1 using the copy constructor."<<endl;
	cout<<"container c2:"<<endl;
	cout<<c2;

	container c3;
	c3=c2;
	cout<<"Declared c3 and assign c2 to c3 with overloaded assignment operator."<<endl;
	cout<<"container c3:"<<endl;
	cout<<c3;
	
	c3.resize(4);
	cout<<"Resized c3 to size = 4."<<endl;
	cout<<"container c3:"<<endl;
	cout<<c3;

	c3.resize(7);
	cout<<"Resized c3 to size = 7."<<endl;
	cout<<"container c3:"<<endl;
	cout<<c3;

	cout<<"Finally, we display the address of three container:"<<endl;
	cout<<"Address of c1 = &c1 = "<<&c1<<endl;
	cout<<"Address of c2 = &c2 = "<<&c2<<endl;
	cout<<"Address of c3 = &c3 = "<<&c3<<endl;
	return 0;
}

program2.cpp: In member function `void container::allocate()':
program2.cpp:125: error: invalid types `int*[int*]' for array subscript
Last edited on
I guess that you think too complicated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void container::allocate()
{
	int new_capacity = capacity*2;
	int *new_data = new int[new_capacity];

	for(int i=0;i<capacity;i++)
	{
	  new_data[i]=data[i];
	}
	delete[] data;

	capacity = new_capacity;
	data = new_data;
}
Topic archived. No new replies allowed.