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

Hello. I'm having trouble with my code. I am trying to use the variables 'nz' and 'ny' in my function 'setCoordinates' but keep getting the error, "initial value of reference to non-const must be an lvalue". The error occurs on the very last line of code I posted. Ie:

r.setCoordinates(nz.getx(), ny.gety());


I have tried to find the solution on here but am still unsure of what to do. Any help would be greatly appreciated. Thank you!


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
point.h


#include <iostream>
using namespace std;

class Point {
public:
	Point();
	double getx();
	double gety();
	void setCoordinates(double &x, double &y);
	double getDistance(double x1, double y1, double x2, double y2);

private:
	double x, y;
};

Point::Point()
{
	x = 0;
	y = 0;
}

double Point::getx()
{
	return x;
}

double Point::gety()
{
	return y;
}

void Point::setCoordinates(double &nx, double &ny)
{
	x = nx;
	y = ny;
}

double Point::getDistance(double x1, double y1, double x2, double y2)
{
	return sqrt((((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1))));
}


rectangle.h


#include <iostream>
using namespace std;

class Rectangle {
public:
	Rectangle(Point x, Point y, Point z);

	Point veritces(Point x, Point y, Point z, Point r);
	double perimeter();
	double area();
	double lowestPoint(Point x, Point y, Point z);
	double highestPoint(Point x, Point y, Point z);

private:
	Point x, y, z, r;
	double distanceXY;
	double distanceYZ;
	double distanceXZ;
}; 

Rectangle::Rectangle(Point nx, Point ny, Point nz)
{
	//Checks if right triangle 
	Point rectangle;
	double distanceYZExact;

	//distance from x to y
	distanceXY = rectangle.getDistance(nx.getx(), nx.gety(), ny.getx(), ny.gety());

	//distance from x to z
	distanceXZ = rectangle.getDistance(nx.getx(), nx.gety(), nz.getx(), nz.gety());

	//distance from y to z
	distanceYZ = rectangle.getDistance(ny.getx(), ny.gety(), nz.getx(), nz.gety());

	//calculates exact distance from y to z
	distanceYZExact = sqrt((distanceXY*distanceXY) + (distanceXZ*distanceXZ));

		if (distanceYZExact != distanceYZ || distanceYZExact == 0)
		{
			cout << "Error, not a right triangle.";
		}
	//calculates last point
		else
			r.setCoordinates(nz.getx(), ny.gety());
}
Last edited on
Why are you taking your parameters by reference on line 35? The results of your method calls are not lvalues and so you can't take references to them. (What variable would be modified if they were changed? The temporary return value?)
My compiler emits several lines of messages regarding this issue:
main.cpp|92|error: no matching function for call to ‘Point::setCoordinates(double, double)’|
main.cpp|92|note: candidate is:|
main.cpp|33|note: void Point::setCoordinates(double&, double&)|
main.cpp|33|note: no known conversion for argument 1 from ‘double’ to ‘double&’|

The last line should be helpful. Why did you define your member function with references? You don't change the values of those parameters perhaps they should be passed by value instead? If you do require the reference parameters then you will need to either change those get() functions to return references (not recommended) or call those functions prior to this location add assign the return values to variables which you then use in set call.

Also this code has other problems you should probably be looking at as well.
||=== Build: Debug in testcpp (compiler: GNU GCC Compiler) ===|
main.cpp||In constructor ‘Point::Point()’:|
main.cpp|17|warning: ‘Point::x’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|17|warning: ‘Point::y’ should be initialized in the member initialization list [-Weffc++]|
main.cpp||In member function ‘double Point::getDistance(double, double, double, double)’:|
main.cpp|41|error: ‘sqrt’ was not declared in this scope|
main.cpp|45|error: ‘rectangle’ does not name a type|
main.cpp||In constructor ‘Rectangle::Rectangle(Point, Point, Point)’:|
main.cpp|68|warning: ‘Rectangle::x’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|68|warning: ‘Rectangle::y’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|68|warning: ‘Rectangle::z’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|68|warning: ‘Rectangle::r’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|68|warning: ‘Rectangle::distanceXY’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|68|warning: ‘Rectangle::distanceYZ’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|68|warning: ‘Rectangle::distanceXZ’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|84|error: ‘sqrt’ was not declared in this scope|
main.cpp|86|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|86|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|92|error: no matching function for call to ‘Point::setCoordinates(double, double)’|
main.cpp|92|note: candidate is:|
main.cpp|33|note: void Point::setCoordinates(double&, double&)|
main.cpp|33|note: no known conversion for argument 1 from ‘double’ to ‘double&’|
main.cpp||In member function ‘double Point::getDistance(double, double, double, double)’:|
main.cpp|42|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 4 error(s), 12 warning(s) (0 minute(s), 0 second(s)) ===|
Also:

The accessor functions should be const.

The parameters to all the functions should also be const.

The Point::setCoordinates function could also be a constructor - have both.

Be careful with line 88, doubles are not exact so one cannot directly compare them to 0.0. This is what jlb's error message was about:

main.cpp|86|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|
main.cpp|86|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|


Google what to do about that.

I always write double values with numbers before and after the decimal point - it's just a reminder that is is a double

Good Luck !!
Thank you to both of you. I don't know why I was passing by reference instead of passing by value. That solved my problem. Also thank you jlb. I will look at those problems as well. :)
Topic archived. No new replies allowed.