Does anyone see my errors?

my errors are:
program2.cpp:70: error: ISO C++ forbids in-class initialization of non-const sta tic member `capacity'
program2.cpp:98: error: prototype for `const container& container::operator=(con st container&)' does not match any in class `container'
program2.cpp:28: error: candidate is: container& container::operator=(const cont ainer&)
program2.cpp:98: error: `const container& container::operator=(const container&) ' and `container& container::operator=(const container&)' cannot be overloaded
program2.cpp: In member function `const container& container::operator=(const co ntainer&)':
program2.cpp:120: error: expected primary-expression before "void"
program2.cpp:120: error: expected `;' before "void"
program2.cpp:129: error: a function-definition is not allowed here before '{' to ken
program2.cpp:129: error: expected `,' or `;' before '{' token
program2.cpp:139: error: expected primary-expression before "void"
program2.cpp:139: error: expected `;' before "void"
program2.cpp:149: error: a function-definition is not allowed here before '{' to ken
program2.cpp:149: error: expected `,' or `;' before '{' token
program2.cpp:158: error: expected primary-expression before "bool"
program2.cpp:158: error: expected `;' before "bool"
program2.cpp:163: error: expected primary-expression before "bool"
program2.cpp:163: error: expected `;' before "bool"
program2.cpp:167: error: expected primary-expression before "int"
program2.cpp:167: error: expected `;' before "int"
program2.cpp:171: error: expected primary-expression before "int"
program2.cpp:171: error: expected `;' before "int"
program2.cpp:176: error: a function-definition is not allowed here before '{' to ken
program2.cpp:176: error: expected `,' or `;' before '{' token
program2.cpp:186: error: a function-definition is not allowed here before '{' to ken
program2.cpp:186: error: expected `,' or `;' before '{' token
program2.cpp:199: error: expected `}' at end of input

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
  // 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;	
	static int capacity=4;		// 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;
}
const 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;
}
Hi,

program2.cpp:70: error: ISO C++ forbids in-class initialization of non-const sta tic member `capacity'


Are you using c++11, it does allow this. Otherwise make it const

Edit: in C++11 it doesn't have to be static, you could just have this:

constexpr std::size_t capacity = 4;

It is the same as if you had that is a member initialization list.

Edit/

program2.cpp:98: error: prototype for `const container& container::operator=(con st container&)' does not match any in class `container'
program2.cpp:28: error: candidate is: container& container::operator=(const cont ainer&)
program2.cpp:98: error: `const container& container::operator=(const container&) ' and `container& container::operator=(const container&)' cannot be overloaded


A const function is a different function to a non-const function. Make sure the declaration / definition match.
1
2
container& operator=(const container &rhs);
const container & container::operator=(const container &rhs)



You are missing a brace on line 116, that might be causing all the other errors. I would have braces even where there is only 1 statement in a loop or whatever. Like on line 107. It will save you one day :+)

Hope this helps :+)
Last edited on
Speaking of member initialization lists: you aren't using them - you should.

http://en.cppreference.com/w/cpp/language/initializer_list


Consider using smart pointers instead of new and delete.

http://en.cppreference.com/w/cpp/memory/unique_ptr
http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
The capacity is changed inside the functions so it should not be const. Each container object should have it's own capacity so you should also remove the static keyword so that it becomes a regular non-const member variable just like count and data.

TheIdeasMan wrote:
Edit: in C++11 it doesn't have to be static, you could just have this:

constexpr std::size_t capacity = 4;

I wanted to try this but my compiler (GCC 4.9.2) says "error: non-static data member ´capacity´ declared ´constexpr´". If I make it both static and constexpr it works fine.

static constexpr std::size_t capacity = 4;
Last edited on
@Peter87

You are right, I knew constexpr happens at compile time, but forgot that a non static variable won't be initialised until the object is instantiated.

Regards:+)
The initialiser of a constexpr object (of a literal type) is evaluated at compile-time;
the value of a non-static member variable of such an object is constexpr.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct A
{
    explicit constexpr A( int i ) : v(i) {}
    
    int v ; // constexpr in a constexpr object of type A
    
    constexpr operator int() const { return v ; }
};

constexpr A object(24) ;

int main()
{
    int array[ object.v ] {} ; // fine; array[ 24 ]
    
    switch( array[2] )
    {
        case object: ++array[2] ; // fine; case 24: 
    }
}

http://coliru.stacked-crooked.com/a/10b87c9104a1d690
Topic archived. No new replies allowed.