Error: initial value of reference to non-const must be an lvalue

Hello there, I've been trying to draw some lines with caclulations, but I've been unable to due to one error I don't understand. (intellisence error(mouseover) is in title)

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
#include <windows.h>
#include <string>
using namespace std;

void ReadFiles(int &id1, int &x1, int &y1, 
	       int &id2, int &x2, int &y2, 
	       char* &t, int &p5)
{
	p5 = 5;
	// other stuff
}

void DrawLine(HDC hdc, int &x, int &y, int &xx, int &yy)
{
	POINT pt[2];
	pt[0].x = x;
	pt[0].y = y;
	pt[1].x = xx;
	pt[1].y = yy;

	Polyline (hdc, pt, 2);
}

void DrawEdge(HDC hdc)
{
	int id1, x1, y1, id2, x2, y2, tx, ty, p5;
	char* t = "";
	ReadFiles(id1, x1, y1, id2, x2, y2, t, p5);
	string treat = t;
	
	if (x1 < x2 && y1 == y2) /*IF toLEFT*/
	{
		DrawLine(hdc, x1, y1, x2, y2);
		// method of adding reference to reference
		DrawLine(hdc, x1, y1, x1+p5, y1+p5);
		DrawLine(hdc, x1, y1, x1+p5, y1-p5);
		TextOut(hdc,tx,ty,t,treat.size());
	}
	else if (x1 == x2 && y1 < y2) /*IF toUP*/
	{
		DrawLine(hdc, x1, y1, x2, y2);
		// method of adding regular int to reference
		DrawLine(hdc, x1, y1, x1+5, y1+5);
		DrawLine(hdc, x1, y1, x1-5, y1+5);
		TextOut(hdc,tx,ty,t,treat.size());
	}
	else MessageBox(NULL, "Diagonal paths not allowed!", "ERROR",
			MB_ABORTRETRYIGNORE | MB_ICONWARNING);
}

both methods give the same errors
main.cpp(35): error C2664: 'DrawLine' : cannot convert parameter 4 from 'int' to 'int &'
main.cpp(36): error C2664: 'DrawLine' : cannot convert parameter 4 from 'int' to 'int &'
main.cpp(43): error C2664: 'DrawLine' : cannot convert parameter 4 from 'int' to 'int &'
main.cpp(44): error C2664: 'DrawLine' : cannot convert parameter 4 from 'int' to 'int &'


is there a way to easily do the x1+5 in the DrawLine without having to do stuff like int xl1a = x1+5; int yl1a = y1+5 every single time?

Thanks in advance!
Rii

EDIT: in short it can't calculate with pass by reference.
Last edited on
You don't need the reference in DrawLine(). Just remove them
It would help if you identified the line of code that the error is on. Without that I'll just speculate.

void DrawLine(HDC hdc, int &x, int &y, int &xx, int &yy)
means the parameters x, y, xx, yy will be modified by DrawLine, so you have to pass variables.

That means you can't write code like:
 
DrawLine(hMyDc, 0, 0, 70, 12);

As DrawLine doesn't actually change these parameters, you should pass them by value, so you get:
1
2
3
4
5
6
7
8
9
10
void DrawLine(HDC hdc, int x, int y, int xx, int yy)
{
	POINT pt[2];
	pt[0].x = x;
	pt[0].y = y;
	pt[1].x = xx;
	pt[1].y = yy;

	Polyline (hdc, pt, 2);
}
You can pass constants to this version of DrawLine.
Last edited on
The error means that you are trying to initialize non-const reference with non lvalue. The problem is that the expressions x1 + p5 and y1 + p5 are not lvalues in the call below.

DrawLine(hdc, x1, y1, x1+p5, y1+p5);

Either you shall declare parameters of the function as const references as, for example,

void DrawLine(HDC hdc, const int &x, const int &y, const int &xx, const int &yy);

or remove references from the parameter declarations because they are not needed.
Last edited on
@all: thank you all for the replies! I removed the references, it's working properly like that now.
bah so foolish of me, sorry for the dumb mistake >_<"
Rii
Topic archived. No new replies allowed.