Adding last element of one list to the end of another

I have 2 global list variables declared like so:
list<Rectangle> pegA;
list<Rectanlge> pegB;
rectangle is a class I made on my own.
During a function I want to add the last element of pegB to the end of pegA. Then remove that last element. This is how I'm doing it:
pegA.push_back(pegB.back());
pegB.pop_back();

But I get this error for push_back:
"error C2663: 'std::vector<_Ty,_Alloc>::back' : 2 overloads have no legal conversion for 'this' pointer"
and this one for pop:
error C2662: 'std::vector<_Ty,_Alloc>::pop_back' : cannot convert 'this' pointer from 'std::vector' to 'std::vector<_Ty,_Alloc> &'

What am I doing wrong?
Other than the misspelling of 'Rectanlge' I see nothing wrong with the snippit you posted.

Can you paste the actual code? It doesn't have to be all of it just these two things:

1) The lines declaring your global pegA and pegB objects

2) The function with your push_back / pop_back lines.
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
#include <iostream>
#include "Rectangle.h"
#include "Colour.h"
#include <list>
#include "EasyBMP.h"
#include "DisplayBackground.h"

void towers(int numDiscs, int presentPeg, int tempPeg, int toPeg);
void DisplayStep();
void ExecuteStep(int from, int to);

list<Rectangle> pegA;
list<Rectanlge> pegB;
list<Rectangle> pegC;
int curStep;
int reqStep;
void DisplayStep();

void main(void)
{
	int n = 0;
	int step = 0;
	int h = 800;
	int w = 600;
	int wDisc = 0;
	int xLoc = 0;
	int yLoc = 0;

	Colour blue = Colour(0,0,255,255);
	Colour green = Colour(0,255,0,255);
	BMP img;
	DisplayBackground(img,green,w,h);

	//prompt the user for the number of pegs
	cout << "Enter number of pegs: " << endl;
	cin >> n;
	cout << "Which step would you like displayed?" << endl;
	cin >> reqStep;

	Rectangle Rtemp;

	//initialize the first peg
	for(int i = 0; i < n; i++)
	{
		wDisc = (int)400*pow(0.8,i);//exponential function
		xLoc = (w-wDisc)/2;
		yLoc = 800-(25*(i+1));
		Rtemp.SetXLocation(xLoc);
		Rtemp.SetYLocation(yLoc);
		Rtemp.SetWidth(wDisc);
		Rtemp.SetHeight(25);
		Rtemp.SetColour(blue);

		pegA.push_back(Rtemp);
		Rtemp.RectangleDisplay(img);
	}
	img.WriteToFile("C:\\Users\\Dave\\Documents\\bitfile.bmp");
	towers(n, 1, 2, 3);

}


void towers(int numDiscs, int presentPeg, int tempPeg, int toPeg)
{
	if(numDiscs > 0)
	{
		curStep++;
		towers(numDiscs-1, presentPeg, toPeg, tempPeg);
		cout << "Move disc from Peg#" << presentPeg << " to Peg#" << toPeg << endl;
		ExecuteStep(presentPeg, toPeg);
		if(curStep==reqStep)
		{
			DisplayStep();
		}
		towers(numDiscs-1, tempPeg, presentPeg, toPeg);
	}
}

void ExecuteStep(int from, int to)
{
	if(from==1 && to==2)
	{
		pegB.push_back(pegA.back());
		pegA.pop_back();
	}
	else if(from==1 && to==3)
	{
		pegC.push_back(pegA.back());
		pegA.pop_back();
	}
	else if(from==2 && to==3)
	{
		pegC.push_back(pegB.back());
		pegB.pop_back();
	}
	else if(from==2 && to==1)
	{
		pegA.push_back(pegB.back());
		pegB.pop_back();
	}
	else if(from==3 && to==1)
	{
		pegA.push_back(pegC.back());
		pegC.pop_back();
	}
	else if(from==3 && to==2)
	{
		pegB.push_back(pegC.back());
		pegC.pop_back();
	}
}

I posted everything just in case. But my errors are coming from the ExecuteStep function.
'Rectanlge' is still spelled wrong (see line 13). That might be your problem.. although I would expect that to throw an error different from the one you described.

Also: EW @ void main. main should always return an int.

EDIT: and wait a minute... you're getting errors with std::vector? but you're not even USING std::vector in that code?

wtf?
Last edited on
Actually those were errors from when I tried to use a vector instead. But anyways that spelling error seemed to be causing all the trouble.

But now I'm getting an error on this:
1
2
3
4
5
6
7
8
9
void DisplayStep()
{
	list<Rectangle>::iterator it;

	for(it = pegA.begin(); it != pegA.end; it++)
	{
		
	}
}

" error C3867: 'std::list<_Ty>::end': function call missing argument list; use '&std::list<_Ty>::end' to create a pointer to member
1> with"

Thanks for help on the other one btw.
"function call missing argument list" is confusing compiler speak for "you forgot your parenthesis"

pegA.end should have parenthesis on it, since it's a function call: pegA.end()
Last edited on
Ah I see. That fixed it. Thanks alot. =]
Topic archived. No new replies allowed.