Invalid initialization of non-const reference of type

I get the following error while I try menu.insert(1, "Iced tea with lemon"); where menu is the Menu object. I have checked everything regarding Text.cpp which is the implementation file for Text.h.


invalid initialization of non-const reference of type ‘Text&’ from an rvalue of type ‘Text’
menu.insert(1, "Iced tea with lemon");

Text.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
#ifndef TEXT_H

#define TEXT_H

#include<iostream>



class Text{

	private:

		char* text;

	public:
		~Text();		// destructor
		Text();			// default constructor
		Text(const Text &);  // copy constructor /constructor overloading
		Text(const char*);// conversion constructor / constructor overloading
		void set(const char*);	// setter
		void set(const Text &);	// overloading setter
		Text & operator= (const Text &);// overloading = operator
		void append (const char*);// appending to Text_store of an object
		void append (const Text &);	// overloading appending to add Text_store of object passed as parameter to the calling object
		int length() const;	// These are read only functions i.e. they can only read the private members and not modify them 
		bool isEmpty() const;				// If a function modifies private members it CANT BE CONST
		friend std::ostream& operator<<(std::ostream & , const Text &); //friend function for operator overloading
};
#endif 


Menu.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
40
41
42
43
44
45
46
#ifndef MENU_H

#define MENU_H

#include "Text.h"

#include <iostream>





class Menu{

	private:

		Text* option_list;							// This is a stack!! Note there are push_back and pop_back functions. So it must be a stack

		int count;

		int max_list_size;

		Text top_prompt;

		Text bottom_prompt;

		Text top_text;

		Text bottom_text;					

	public:

		Menu();	// Default Constructor
		Menu(const Menu &);// Copy Constructor
		~Menu();

                //some other functions

		void insert(int, Text &);// Insert at position by shifting

		//Operator Overloading
		Menu & operator= (const Menu &);
		friend std::ostream& operator<<(std::ostream & , Menu &); 
};

#endif  
You're going to need to post your member function definitions as well.


Why are you using an array of char in your class instead of a std::string?
Text.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "Text.h"

#include <iostream>

#include<cstring>

//using namespace std;







// destructor

Text::~Text(){

	delete[] this->text;

}



// Default Constructor

Text:: Text(){

	this->text = new char;  				//c++ cant print null pointer

}



// copy constructor /constructor overloading

Text::Text(const Text &Text){





	//delete 1

	this->text = new char[strlen(Text.text) + 1];		// obviously points to nothing so allocate dynamic memory allocation with same size as that of passed arg

	strcpy(this->text,Text.text);				// copy the text

}



// conversion constructor / constructor overloading

Text::Text(const char* text){





	//delete

	this->text = new char[strlen(text) + 1];		// obviously points to nothing so allocate dynamic memory and copy

	strcpy(this->text,text);

}



// setter

void Text::set(const char* text){

	if (this->text == NULL){				// if NULL then allocate dynamic memory and copy





		//delete 1

		this->text = new char[strlen(text) + 1];

		strcpy(this->text,text);

	}else{							// if not NULL then deallocate, allocate dynamic memory and copy

		delete[] this->text;



		//delete 1

		this->text = new char[strlen(text) + 1];

		strcpy(this->text,text);

	}

}



// overloading setter

void Text::set(const Text &Text){

	delete[] this->text;					// deallocate, allocate, copy



	//delete 1

	this->text = new char[strlen(Text.text) + 1];

	strcpy(this->text,Text.text);

}



// overloading = operator

Text & Text::operator=(const Text &Text){			// Note the Precedence of this operator is from RIGHT to LEFT

	set(Text);						// could make this faster by checking if both have the same string

	return *this; 						//A question....what if i return only this. thats not possible coz of const keyword.

								// Why cant we use &this as return. If an object calls it also passes itself as an argument(obviously by 									//reference). But then *this would be the calling object not its reference.

								// But when we return it is converted back to reference coz of Text & at return type.

}



// appending to Text_store of an object

void Text::append (const char* text){

	// Store this->text in temp and deallocate this->text

	char *temp = new char[strlen(this->text)];

	strcpy(temp,this->text);

	delete[] this->text;



	// Allocate memory for new string

	//delete 1

	int len = strlen(temp) + strlen(text) + 1;

	this->text = new char[len];



	// Copy temp in this->text. Then concatinate text to this->text

	strcpy(this->text,temp);

	strcat(this->text,text);



	// deallocate temp

	delete [] temp;

}



// overloading appending to add Text_store of object passed as parameter to the calling object

void Text::append(const Text &Text){

	// Store this->text in temp and deallocate this->text

	char *temp = new char[strlen(this->text)];

	strcpy(temp,this->text);

	delete[] this->text;



	// Allocate memory for new string



	//delete 1

	int len = strlen(temp) + strlen(Text.text) + 1;

	this->text = new char[len];



	// Copy temp in this->text. Then concatinate text to this->text

	strcpy(this->text,temp);

	strcat(this->text,Text.text);



	// deallocate temp

	delete [] temp;



}



// These are read only functions i.e. they can only read the private members and not modify them

int Text::length() const{

	return strlen(this->text);

}



// These are read only functions i.e. they can only read the private members and not modify them

bool Text::isEmpty() const{

	return (this->length()==0 ? true:false);

}



std::ostream& operator<<(std::ostream& os, const Text &Text){ 		//friend function for operator overloading

	if(Text.text==NULL){						//had to do this because c++ cant print null pointer

		os << "";

	}else{

		os << Text.text;

		return os;

	}

}



/*

int main()

{

	Text t1("Welcome to C++"); 						// conversion constructor 4

	Text t2; 										// defalt constructor

	Text t3(t1); 									// copy constructor

	cout << "t1: " << t1 << endl; 					// operator<< overload 7

	cout << "t2: " << t2 << endl;

	cout << "t3: " << t3 << endl;

	t2.set(" Programming"); 						// set t2's text to " Programming" 11

	cout << "t2: " << t2 << endl;

	t3.set(t1); 									// set t3's text to t1's text 14

	cout << "t3: " << t3 << endl;

	t1.append(" Programming"); 						// append the c-string " Programming" to t1's text 17

	cout << "t1: " << t1 << endl;

	t3.append(t2); 									// append t2's text to t3's text 20

	cout << "t3: " << t3 << endl;

	cout<< t3.isEmpty()<<endl;

	cout<<t2.length()<<endl;



	Text t4;

	t4 = t3 = t2;

	Text t5;





	t2.append("?->");



	cout << t2;





	return 0;

}

*/
Menu.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652

#include "Text.h" 

#include "Menu.h"

#include <iostream>

#include <cstring>

#include<string>

#include<stdexcept>

#include<cstdlib>

using namespace std;





//Destructor

Menu::~Menu(){

	delete [] this->option_list;

}





// Default Constructor

Menu::Menu(){

	this->count = 0;

	this->max_list_size = 1;

	this->option_list = new Text[max_list_size]; // This is a stack!! Note there are push_back and pop_back functions. So it must be a stack

	this->top_prompt = "";

	this->bottom_prompt = "";

	this->top_text = "";

	this->bottom_text = "";

}





// Copy Constructor

// NEED TO CHECK FOR DEEP COPY	for all variables

Menu::Menu(const Menu &otherMenu){

	this->count = otherMenu.count;

	this->max_list_size = otherMenu.max_list_size;



	// Allocate memory for stack of the created object

	this->option_list = new Text[otherMenu.count];



	// Copy the stack from otherMenu

	for (int a=0; a<=count - 1; a++){

		this->option_list[a] = otherMenu.option_list[a];

	}



	this->top_prompt = otherMenu.top_prompt;

	this->bottom_prompt = otherMenu.bottom_prompt;

	this->top_text = otherMenu.top_text;

	this->bottom_text = otherMenu.bottom_text;



}







// Double max_list_size [little modification as per assignment expectation]

void Menu::double_capacity(){

	//double the size

	this->max_list_size = this->max_list_size * 2;



	//make temp array for copying

	Text *temp_copy = new	Text[this->max_list_size];



	//Copy this->option_list to temp_copy

	for (int i = 0; i <=(this->count -1); i++) {

		temp_copy[i] = this->option_list[i];

	}



	//Deallocate this->option_list

	delete []this->option_list;



	//Allocate memory to this->option_list with size this->max_list_size

	this->option_list = new Text[this->max_list_size];



	//TEST

	cout << this->max_list_size<< endl;



	//Copy content to this->option_list from temp_copy

	for (int i = 0; i <=(this->count -1); i++) {

		this->option_list[i] = temp_copy[i] ;

	}



	//Deallocate temp_copy

	delete [] temp_copy;





}





// Create text obj assign char * and PUSH it to the STACK option_list

void Menu::push_back(char *pOption){

	// Check for overflow

	if (this->count == this->max_list_size){

		this->double_capacity();

	}



	//If not overflow

	this->option_list[this->count].set(pOption);

	this->count = this->count + 1;

	return;



}





// PUSH text obj to the STACK option_list

void Menu::push_back(const Text &option){

	// check OVERFLOW

	if (this->count == this->max_list_size){

		this->double_capacity();

	}



	//If not overflow

	this->option_list[this->count] = option;

	this->count = this->count + 1;

	return;

}





// Insert at position by shifting

void Menu::insert(int index, Text &option){

	//Check if index given is negative

	if(index<0){

		cout << "No negative indexing" << endl;

	}



	//Check if index given is valid

	if ((index - 1) > this->count){

		cout << "Index out of bounds" <<endl;

	}



	//Check if there is space in stack for insertion

	if(this->count == this->max_list_size){

		cout<< "Stack is full. Increase size to insert" << endl;

	}



	//Make a copy and insert

	//Text temp = this->option_list[index-1];



	Text *new_option_list = new Text[this->max_list_size];



	int new_index = 0;



	for (int c = 0 ; c <= (this->max_list_size - 1); c++){		// -1 because max_list_size if 4 then index is 0, 1, 2, 3



		// If at index-1 copy option

		if (c == (index - 1)){

			new_option_list[c] = option;

			new_index = new_index + 1;

			continue;

		}



		// Copy from this->option_list to new_option_list

		new_option_list[new_index] = this->option_list[c];

		new_index = new_index+1;

	}



	//deallocate this->option_list

	delete [] this->option_list;



	//allocate memory to this->option_list

	this->option_list = new Text[this->max_list_size];



	//Now copy from new_option_list to new this->max_list_size

	for (int c = 0 ; c <= (this->count - 1) ; c++){

		this->option_list[c] =  new_option_list[c];

	}



	//increase count

	this->count = this->count + 1;



}





//No idea if NULL pointer can be returned

// removes option from STACK option_list at index i

Text Menu::remove(int index){

	//Check if stack is empty

	if(this->isEmpty()){

		cout << "Nothing to remove" << endl;

		Text t;

		return t;

	}

	//Check if index given is negative

	if(index<0){

		cout << "No negative indexing" << endl;

		Text t;

		return t;

	}



	//Check if index given is valid

	if ((index - 1) > this->count){

		cout << "Index out of bounds" <<endl;

		Text t;

		return t;

	}





	//If index is not out of bounds

	Text temp = this->option_list[index-1];



	Text *new_option_list = new Text[this->max_list_size];



	int new_index = 0;



	for (int c = 0 ; c < (this->max_list_size - 1); c++){		// -1 because max_list_size if 4 then index is 0, 1, 2, 3



		// Skip this because we dont want to copy this to the new array

		if (c == (index - 1)){

			continue;

		}



		// Copy from this->option_list to new_option_list

		new_option_list[new_index] = this->option_list[c];

		new_index = new_index+1;

	}



	//deallocate this->option_list

	delete [] this->option_list;



	//allocate memory to this->option_list

	this->option_list = new Text[this->max_list_size];



	//Now copy from new_option_list to new this->max_list_size

	for (int c = 0 ; c <= (this->count - 1) ; c++){

		this->option_list[c] =  new_option_list[c];

	}



	//update this->count

	this->count = this->count - 1;



	return temp;



}





// Returns size of STACK option_list NOTE THIS IS NOT max_list_size

const int Menu::size(){

	return this->count;

}





// Returns value of max_list_size

const int Menu::capacity(){

	return this->max_list_size;

}





// POP from STACK max_list_size

void Menu::pop_back(){

	if (this->isEmpty()){

		cout<< "There is nothing to remove" << endl;

	}else{

		this->remove(this->count - 1);

	}

}





// Return k th From stack option_list

Text Menu::get(int k){

	//if empty send message

	if(this->isEmpty()){

		cout<< "Empty stack"<< endl;

		Text t;

		return t;

	}else{

		return this->option_list[k-1];

	}

}



// Read all in STACK option_list and concatinate them in char*, store in Text object and return it

const Text Menu::toString(){

	// Create a Text object to store the string representation of Menu object. This is the object that we will return;

	Text toStr;



	// Checking if all the members are empty

	if (this->count==0 && this->top_text.isEmpty() && this->top_prompt.isEmpty() && this->bottom_text.isEmpty() && this->bottom_prompt.isEmpty()){

		// Only append literal

		toStr.append("?->");

		return toStr;

	}





	//append top_message

	toStr.append(this->top_text);

	toStr.append("\n");



	//append top_prompt

	toStr.append(this->top_prompt);

	toStr.append("\n");





	//append all Text object in option_list array of Menu

	for (int c = 0; c <= (this->count - 1); c++){



		char *conc = new char[4];

		conc[0] = '(';

		conc[1] = '0' + (c+1);						//Convert to char from ASCII

		conc[2] = ')';



		cout<<conc;



		//append conc and option_list[c] to toString Text object

		toStr.append(conc);

		toStr.append(this->option_list[c]);

		//append next line escape sequence

		toStr.append("\n");

	}



	/*

	//string literal

	char *literal = "?->";

	*/



	//append string literal

	toStr.append("?->");



	//append bottom_prompt

	toStr.append(this->bottom_prompt);

	toStr.append("\n");



	//append bottom_message

	toStr.append(this->bottom_text);

	//toString.append("\n");



	//deallocate

	//delete [] conc;



	return toStr;

}


This is not complete code the rest will follow in next section just copy and paste it at then end of this code.
copy this just after } in the code above
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

// Calls to string internally then reads and returns a vaiid option number

int Menu::read_option_number(){



	int option;

	char *user_choice = new char[256];

	bool condition = true;



	//if empty just return choice

	if(this->count == 0){

		while (condition){

			cin.getline(user_choice,256);

			try{

				option = atoi(user_choice);

				condition = false;

			}catch(invalid_argument &e){

				cout << "Invalid entry, must be a number" << endl;

			}

		}



		delete []user_choice;

		return option;

	}



	cout << this->toString();





	while (condition){

		cin.getline(user_choice,256);

		try{

			option = atoi(user_choice);

			condition = false;

		}catch(invalid_argument &e){

			cout << "Invalid entry, must be a number" << endl;

		}catch(out_of_range &e){

			cout << "Wahahahahahahahah!!! art thou SERIOUS??" << endl;

		}



		if(option > this->count){

			cout << "invalid choice" << option << ". It must be within range [1," << this->count << "]" << endl;

			condition = true;

		}

	}

	delete []user_choice;

	return option;

}





//I think these are explained in a wrong way in the assignment

void Menu::set_top_prompt(const Text &t){

	this->top_prompt = t;

}





void Menu::set_bottom_prompt(const Text &t){

	this->bottom_prompt = t;

}





void Menu::set_top_message(const Text &m){

	this->top_text = m;

}





void Menu::set_bottom_message(const Text &m){

	this->bottom_text = m;

}



void Menu::clear_top_prompt(){

	this->top_prompt.set("");

}



void Menu::clear_bottom_prompt(){

	this->bottom_prompt.set("");

}





void Menu::clear_top_message(){

	this->top_text.set("");

}





void Menu::clear_bottom_message(){

	this->bottom_text.set("");

}





const bool Menu::isEmpty(){                       // Returns true if STACK option_list is empty()

	if (this->count == 0){

		return true;

	}else{

		return false;

	}

}





//Operator Overloading

Menu & Menu::operator= (const Menu &menu){

	//deallocate option_list

	delete [] this->option_list;



	//allocate memory

	this->option_list = new Text[menu.max_list_size];



	//copy stack option_list

	for(int c=0; c<=menu.count - 1; c++){

		this->option_list[c] = menu.option_list[c];

	}



	//copy all other members

	this->count = menu.count;

	this->max_list_size = menu.max_list_size;

	this->top_prompt = menu.top_prompt;

	this->bottom_prompt = menu.bottom_prompt;

	this->bottom_text = menu.bottom_text;

	this->top_text = menu.top_text;



	return *this;

}



//need tostring for this.

std::ostream& operator<<(std::ostream &os , Menu &menu){

	os << menu.toString();

	return os;

}



/*

int main(){

	Menu m;

	cout << m;

};

*/

I get the following error while I try menu.insert(1, "Iced tea with lemon"); where menu is the Menu object. I have checked everything regarding Text.cpp which is the implementation file for Text.h.


invalid initialization of non-const reference of type ‘Text&’ from an rvalue of type ‘Text’
menu.insert(1, "Iced tea with lemon");


Is these the only error messages you are getting?

If not please post all of your error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the problems. One of these important embedded messages is the line number where the problem was encountered.

And remember you need to start at the first reported warning or error, fix it and then recompile. Many times a single error can cause multiple errors or warnings.

Hi,

IMO, this line should be

void Menu::insert(const int index, const Text& option){


invalid initialization of non-const reference of type ‘Text&’ from an rvalue of type ‘Text’
menu.insert(1, "Iced tea with lemon");


Probably because of this line 270, given that Text is an parameter to the function:

Text *new_option_list = new Text[this->max_list_size];

Use a different variable name there. But then you don't really that variable in the function anyway.

I think you are over using this-> the this pointer is convenient in operator overloading, but not necessary here.

You could avoid having the new and delete operators, just use a std::vector. Using new just to obtain a pointer is not a good thing, use the STL where ever you can. The STL containers put their data on the heap and do their own memory management. The biggest problem with new, is if something throws an exception, delete is never reached.
Last edited on
Topic archived. No new replies allowed.