Please can someone assist...

I was requested to modify some code to add some functionality, and when I was validating the inital code I am getting the following error:

1>d:\school\cis326\week 8\week 8\week 8\week 8.cpp(119): error C2662: 'Linear_regress::perform_regression' : cannot convert 'this' pointer from 'const Linear_regress' to 'Linear_regress &'
1> Conversion loses qualifiers

Here are the code that we were given, I believe I have highlighted the area giving me the issues:

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
#include <iostream>
#include <fstream>
using namespace std;

class Point
{
public:
	double x, y;
};
class Line
{
public:
	double m, b;
};
class Linear_regress
{
private:
	const Point* fit_pts;
	Line fit_line;
	int num_fit_pts;

public:
	void perform_regression (const Point*, int, int);
};
class Traffic
{
private:
	enum {MAX_NUM_ACCID_PTS=200};
	Point accid_pts[MAX_NUM_ACCID_PTS];
	int index_at_max_accid, num_accid_pts;
	const Linear_regress& regression_reference;
public:
	Traffic (const Linear_regress&);
	void read_data ();
	void find_index_at_max_accid ();
	void calc_lines ();
};

void Linear_regress::perform_regression(const Point* array_beginning,
	int low_subscript, int high_subscript)

{
	double c, d, e, f, n;
	int i;

	c=d=e=f=0.0;

	num_fit_pts = high_subscript - low_subscript + 1;
	fit_pts = array_beginning + low_subscript;

	for (i = 0; i < num_fit_pts; i++)
	{
		c += fit_pts[i].x;
		d += fit_pts[i].y;
		e += fit_pts[i].x * fit_pts[i].x;
		f += fit_pts[i].x * fit_pts[i].y;
	}
		n = num_fit_pts;
		fit_line.m = (n * f - c * d) / (n * e - c * c);
		fit_line.b = (d * e - c * f) / (n * e - c * c);

		cout<<"The best fit line: "<<endl<<"m="<<fit_line.m<<endl
			<<" b="<<fit_line.b<<endl<<endl;
}

Traffic::Traffic (const Linear_regress& regress_ref_var)
	:regression_reference (regress_ref_var),
	index_at_max_accid(0), num_accid_pts(0)
{
	int i;
	for (i=0; i<MAX_NUM_ACCID_PTS; i++)
	{
		accid_pts[i].x=0;
		accid_pts[i].y=0;
	}
}

void Traffic::read_data()
{
	int i;

	ifstream infile("D:\\School\\CIS326\\Week 8\\BESTLINE.DAT");
	
		i=0;
		do
		{
			infile>>accid_pts[i].x;
			infile>>accid_pts[i].y;
			i++;
		} while (!infile.eof());
		num_accid_pts = i - 1;
}

void Traffic::find_index_at_max_accid ()
{
	int i;
	double max;
	max = accid_pts[0].y;
	for (i = 1; i < num_accid_pts; i++)
	{
		if (accid_pts[i].y >= max)
		{
			max = (accid_pts[i].y);
			index_at_max_accid = i;
		}
	}

}

void Traffic::calc_lines ()
{
	cout<<"Line before maximum accidents."<<endl;
	regression_reference.perform_regression(accid_pts, 0, 
		index_at_max_accid);

	cout<<"Line after maximum accidents."<<endl;
	regression_reference.perform_regression (accid_pts, index_at_max_accid,
		num_accid_pts-1);
}

int main ()
{
	Linear_regress regression_object;
	Traffic			monthly_traffic (regression_object);

	monthly_traffic.read_data ();
	monthly_traffic.find_index_at_max_accid ();
	monthly_traffic.calc_lines ();
}
Last edited on
Could you please edit your orig post to use a "code" tag

"How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/

Your code is rather difficult to read at the mo', so I'm not sure. But somewhere (well, line 119 - but I don't now where that line is) you are trying to call a non-const method through a const ref.

Prob this line, in Traffic::calc_lines()

regression_reference.perform_regression( ... );

As this method is changing the state of the object, it cannot be changed to const.

Andy
Last edited on
I apologize, I am still learning this site and that suggestion is greatly appreciated. I have however resolved that issue and now I am trying to have the user enter a value for MAX_NUM_ACCID_PTS on line 35 and I have the following code and am stuck on why it is telling me there is no memory for the value. It is just a int that I am trying to prompt the user for...

Here is the code:
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
#include <iostream>
#include <fstream>
using namespace std;

class Point
{
	public:
		double x,y;
};

class Line
{
	public:
		double m, b;
};

class Linear_regress
{
	private:
		//const Point* fit_pts;
		Point* fit_pts;
		Line fit_line;
		int num_fit_pts;


	public:
		//void perform_regression(const Point*, int, int);
		void perform_regression(Point*, int, int);
};

class Traffic

{
	private:
		int max_pts;
		cout<<"Enter the number of accident points: "<<endl;
		cin>>max_pts;
		enum MAX_NUM_ACCID_PTS(max_pts);
		Point accid_pts[MAX_NUM_ACCID_PTS];
		int index_at_max_accid, num_accid_pts;
		//const Linear_regress& regression_reference;
		Linear_regress& regression_reference;
	public:
		//Traffic(const Linear_regress&);
		Traffic(Linear_regress&);
		void read_data();
		void find_index_at_max_accid();
		void calc_lines();
};

//void Linear_regress::perform_regression(const Point* array_beginning, 
void Linear_regress::perform_regression(Point* array_beginning, 
int low_subscript, int high_subscript)






{
	double c, d, e, f, n;
	int i;

	c=d=e=f=0.0;


	num_fit_pts=high_subscript-low_subscript+1;
	fit_pts=array_beginning+low_subscript;

	
	
	
	

	for (i=0; i<num_fit_pts; i++)
		{
		c += fit_pts[i].x;
		d += fit_pts[i].y;
		e += fit_pts[i].x*fit_pts[i].x;
		f += fit_pts[i].x*fit_pts[i].y;
		}

	n = num_fit_pts;
	fit_line.m = (n*f - c*d)/(n*e - c*c);
	fit_line.b = (d*e - c*f)/(n*e - c*c);





	cout<<"The best fit line:"<<endl<<"   m="<<fit_line.m<<endl<<
			"   b="<<fit_line.b<<endl<<endl;

}

//Traffic::Traffic(const Linear_regress& regress_ref_var)
Traffic::Traffic(Linear_regress& regress_ref_var)
            : regression_reference (regress_ref_var), 
index_at_max_accid(0), num_accid_pts(0)
{
	int i;
	for (i=0; i<MAX_NUM_ACCID_PTS; i++)
		{
		accid_pts[i].x=0;
		accid_pts[i].y=0;
		}
}

void Traffic::read_data()
{
	int i;

	ifstream infile("D:\\School\\CIS326\\Week 8\\BESTLINE.DAT");

	i=0;
	do
		{
		infile>>accid_pts[i].x;
		infile>>accid_pts[i].y;
		i++;
		}   while (!infile.eof());

	num_accid_pts = i-1;
}


void Traffic::find_index_at_max_accid()
{
	int i;
	double max;

	max = accid_pts[0].y;
	for (i=1; i<num_accid_pts; i++)
		{
		if (accid_pts[i].y >= max)
			{
			max = accid_pts[i].y;
			index_at_max_accid = i;
			}
		}

}



void Traffic::calc_lines()
{
	cout<<"Line before maximum accidents."<<endl;
	regression_reference.perform_regression(accid_pts, 0, index_at_max_accid);

	cout<<"Line after maximum accidents."<<endl;
	regression_reference.perform_regression(accid_pts, index_at_max_accid, num_accid_pts-1);
}




int main ( )
{
	Linear_regress	regression_object;
	Traffic		monthly_traffic(regression_object);

	monthly_traffic.read_data();
	monthly_traffic.find_index_at_max_accid();
	monthly_traffic.calc_lines();

	system("pause");
}
Last edited on
Hmmm...

enum MAX_NUM_ACCID_PTS(max_pts);

This statement is invalid. enums don't work like that!

As written, you're almost defining a enum type MAX_NUM_ACCID_PTS. But not quite.

You could use enum to define a single (const) value like:

enum MAX_NUM_ACCID_PTS { max_pts = 1000 }; // now with curly brackets

which would allow you to define a variable like

MAX_NUM_ACCID_PTS value = max_pts;

or an array like

Point accid_pts[max_pts];

But as I said, MAX_NUM_ACCID_PTS isn't enum value, it's a type, so you can't use it like

Point accid_pts[MAX_NUM_ACCID_PTS];

I think you need to read up about enums. In short, they are a special type of constant. A way of defining group of named constants. Once defined they cannot be changed.

e.g.

enum RainbowColors
{
red, orange, yellow, green, blue, indigo, violet
};

Here, as I have not provided specific values, the enum values are automatically assigned values 0, 1, 2, ... (i.e. here, red = 0, orange = 1, yellow = 2, ...)

Also, C++ arrays must be declared with a constant size (some non-standard compilers ight allow this, but it's best to avoid). So if you need to an array size at runtime, you will have to new it.
In fact, this sequence starting at line 35 is all a bit broken. You need to move the cout, cin, etc. to a class meber function.

1
2
3
4
5
		int max_pts;
		cout<<"Enter the number of accident points: "<<endl;
		cin>>max_pts;
		enum MAX_NUM_ACCID_PTS(max_pts);
		Point accid_pts[MAX_NUM_ACCID_PTS];


As I said before, if you want to allocated storage for a user provided number of points, you can't use a C array. You need to rework the last line to work with new.

Check up how class member functions are defined, as opposed to member variables.
Last edited on
Ok, so I believe that I am going in the right direction and just wanted to make sure. RIght now I am having a issue calling the Point.MAX_NUM_ACCID_PTS, the error is telling me that it must be a constant... Not sure where I made that mistake as of yet but was just making sure of the progress before I go too far down this road.

Here is the update:
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
#include <iostream>
#include <fstream>
using namespace std;

class Point
{
	public:
		double x,y;
		int MAX_NUM_ACCID_PTS;
		
int main ()
	{
		cout<<"Enter the number of accident points: "<<endl;
		cin>>MAX_NUM_ACCID_PTS;
	}

};

class Line
{
	public:
		double m, b;
};

class Linear_regress
{
	private:
		//const Point* fit_pts;
		Point* fit_pts;
		Line fit_line;
		int num_fit_pts;


	public:
		//void perform_regression(const Point*, int, int);
		void perform_regression(Point*, int, int);
};

class Traffic

{
	private:
		Point accid_pts[Point.MAX_NUM_ACCID_PTS];
		int index_at_max_accid, num_accid_pts;
		//const Linear_regress& regression_reference;
		Linear_regress& regression_reference;
	public:
		//Traffic(const Linear_regress&);
		Traffic(Linear_regress&);
		void read_data();
		void find_index_at_max_accid();
		void calc_lines();
		
};

//void Linear_regress::perform_regression(const Point* array_beginning, 
void Linear_regress::perform_regression(Point* array_beginning, 
int low_subscript, int high_subscript)






{
	double c, d, e, f, n;
	int i;

	c=d=e=f=0.0;


	num_fit_pts=high_subscript-low_subscript+1;
	fit_pts=array_beginning+low_subscript;

	
	
	
	

	for (i=0; i<num_fit_pts; i++)
		{
		c += fit_pts[i].x;
		d += fit_pts[i].y;
		e += fit_pts[i].x*fit_pts[i].x;
		f += fit_pts[i].x*fit_pts[i].y;
		}

	n = num_fit_pts;
	fit_line.m = (n*f - c*d)/(n*e - c*c);
	fit_line.b = (d*e - c*f)/(n*e - c*c);





	cout<<"The best fit line:"<<endl<<"   m="<<fit_line.m<<endl<<
			"   b="<<fit_line.b<<endl<<endl;

}

//Traffic::Traffic(const Linear_regress& regress_ref_var)
Traffic::Traffic(Linear_regress& regress_ref_var)
            : regression_reference (regress_ref_var), 
index_at_max_accid(0), num_accid_pts(0)
{
	int i;
	for (i=0; i<Point.MAX_NUM_ACCID_PTS; i++)
		{
		accid_pts[i].x=0;
		accid_pts[i].y=0;
		}
}

void Traffic::read_data()
{
	int i;

	ifstream infile("D:\\School\\CIS326\\Week 8\\BESTLINE.DAT");

	i=0;
	do
		{
		infile>>accid_pts[i].x;
		infile>>accid_pts[i].y;
		i++;
		}   while (!infile.eof());

	num_accid_pts = i-1;
}


void Traffic::find_index_at_max_accid()
{
	int i;
	double max;

	max = accid_pts[0].y;
	for (i=1; i<num_accid_pts; i++)
		{
		if (accid_pts[i].y >= max)
			{
			max = accid_pts[i].y;
			index_at_max_accid = i;
			}
		}

}



void Traffic::calc_lines()
{
	cout<<"Line before maximum accidents."<<endl;
	regression_reference.perform_regression(accid_pts, 0, index_at_max_accid);

	cout<<"Line after maximum accidents."<<endl;
	regression_reference.perform_regression(accid_pts, index_at_max_accid, num_accid_pts-1);
}




int main ( )
{
	Linear_regress	regression_object;
	Traffic		monthly_traffic(regression_object);

	monthly_traffic.read_data();
	monthly_traffic.find_index_at_max_accid();
	monthly_traffic.calc_lines();

	system("pause");
}


Thank you again for all the assistance.
Last edited on
Topic archived. No new replies allowed.