Error from stl_vector.h

Hi,
I'm trying to program an algorithm I found in an article from Guibas and Oudot.
I've just learnt C++ for this special occasion...
Here is my 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
// Imports
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

inline string remove_extension(string &s) {
	size_t lastdot = s.find_last_of('.');
    if (lastdot == string::npos) return s;
    return s.substr(0, lastdot);
}

int distance(vector<int> *p1, vector<int> *p2) {
	return ((*p1)[0] - (*p2)[0]) * ((*p1)[0] - (*p2)[0]) + ((*p1)[1] - (*p2)[1]) * ((*p1)[1] - (*p2)[1]);
}

int distance(vector<int> *p, vector< vector<int>* > &L) {
	size_t l = L.size();
	int d = distance(p, L[0]);
	for (int k = 0; k < l; ++k) {
		int dk = distance(p, L[k]);
		if (dk < d) d = dk;
	}
	return d;
}

int farthest(vector< vector<int> > &points, vector< vector<int>* > &L) {
	size_t p = points.size();
	size_t l = L.size();
	int m = 0;
	int dm = distance(&(points[m]), L);
	for (size_t j = 1; j < p; ++j) {
		int dj = distance(&(points[j]), L);
		if (dm < dj) {
			m = j;
		}
	}
	return m;
}

void update_neighbours(vector< vector<int> > &points, vector< vector< vector<int>* > > neighbours, vector< vector<int>* > &L, vector<int> *last) {
	size_t n = points.size();
	for (int i = 0; i < n; ++i) {
		vector<int> **nearest1 = &(neighbours[i][0]);
		vector<int> **nearest2 = &(neighbours[i][1]);
		int d1 = distance(&(points[i]), *nearest1);
		int d2 = distance(&(points[i]), *nearest2);
		vector<int> **farthest = (d1 > d2 ? nearest1 : nearest2);
		int d = distance(&(points[i]), *farthest);
		if (d > distance(&(points[i]), last)) {
			*farthest = last;
		}
	}
}

void reconstruction(vector< vector<int> > &points, ofstream &ofile) {
	size_t n = points.size();
	vector< vector<int>* > L;
	L.push_back(&(points[0]));
	vector< vector< vector<int>* > > neighbours(n, (vector< vector<int>* >){2, L[0]});
	vector< vector< vector<int>* > > CWL;
	vector<int> *first = L[0];
	size_t i = 1;
	while (i < n) {
		int j = farthest(points, L);
		vector<int> *cur = &(points[j]);
		L.push_back(cur);
		update_neighbours(points, neighbours, L, cur);

		if (i == 1) {
			CWL.push_back((vector< vector<int>* >){L[0], cur});
			continue;
		}
		else if (i == 2) {
			CWL.push_back((vector< vector<int>* >){L[0], cur});
			CWL.push_back((vector< vector<int>* >){L[1], cur});
			continue;
		}

		ofile << "Reconstruction n. " << i << " : " << endl;
		vector<int> *nearest1 = neighbours[j][0];
		vector<int> *nearest2 = neighbours[j][1];

		CWL.push_back(vector< vector<int>* > {nearest1, cur});
		CWL.push_back(vector< vector<int>* > {nearest2, cur});
		size_t m = CWL.size();
		for (size_t k = 0; k < m; ++k) {
			if ((CWL[k][0] == nearest1 or CWL[k][0] == nearest2) and (CWL[k][1] == nearest1 or CWL[k][1] == nearest2)) {
				CWL[k] = vector< vector<int>* > {NULL, NULL};
			}
			else if (CWL[k][0] != NULL) {
				ofile << (*(CWL[k][0]))[0] << " , " << (*(CWL[k][0]))[1]
					  << " ; "
					  << (*(CWL[k][1]))[0] << " , " << (*(CWL[k][1]))[1] << endl;
			}
		}
	}
}

int main(int argc, char **argv) {
	if (argc <= 1) {
		cout << "You need to specify a file";
	}
	if (argc > 2) {
		cout << "There is only one argument to be specified";
	}
	ifstream ifile(argv[1], ios::in);

	string name = argv[1];
	name = remove_extension(name);
	name = name + (string)".cplx";
	ofstream ofile(name.c_str(), ios::out | ios::trunc);

	string dim;
	getline(ifile, dim);
	if (dim != "2") {
		cout << "This algorithm does only work for dimension 2";
	}

	vector< vector<int> > points;
	size_t i = 0;
	while (!ifile.eof()) {
        vector<int> point(2, 0);
		points.push_back(point);
		int x, y;
		ifile >> x;
		ifile >> y;
        points[i][0] = x;
        points[i][1] = y;
		i++;
	}
	reconstruction(points, ofile);
	return 0;
}


It generates an error from stl_vector.h and not from my piece of code ! How can I manage to debug that ?

Thank you,
Pierre E.
It generates an error from stl_vector.h and not from my piece of code !

Can you be more specific about the error generated?
That was an invalid conversion of types. But I can't remember the error exactly and I'm not with my pc right now, anyway should get the same error when compiling it as this is my only file.
Compound-literals (something that looks like a C-style cast of an initialiser) are permitted in C, but not in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct A { int x ; double y ; } ;

void foo( struct A a ) ;

int main()
{
    foo( (struct A){ 23, 7.6 } ) ; // valid C, invalid C++

    typedef struct A A ;
    foo( (A){ 23, 7.6 } ) ; // valid C, invalid C++

    foo( A{ 23, 7.6 } ) ; // valid C++, invalid C

    foo( { 23, 7.6 } ) ; // valid C++, invalid C
}


There are compound-literals in many places in your code; for instance, line 61:
vector< vector< vector<int>* > > neighbours(n, (vector< vector<int>* >){2, L[0]});

To fix these errors, remove the parentheses around the type.
vector< vector< vector<int>* > > neighbours(n, vector< vector<int>* >{2, L[0]});
My compiler complains about the parentheses you have used when constructing the vectors you pass to push_back.

 
CWL.push_back((vector< vector<int>* >){L[0], cur});


This should work better

 
CWL.push_back(vector< vector<int>* >{L[0], cur});


In fact, you don't even need to state the type you are constructing because it can be inferred from the parameter type of the push_back function.

 
CWL.push_back({L[0], cur});
Well, I changed all that but it did only remove some warnings. I still have the error from stl_vector.h...
Here is the code now :
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
// Imports
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

inline string remove_extension(string &s) {
	size_t lastdot = s.find_last_of('.');
	if (lastdot == string::npos) return s;
	return s.substr(0, lastdot);
}

int distance(vector<int> *p1, vector<int> *p2) {
	return ((*p1)[0] - (*p2)[0]) * ((*p1)[0] - (*p2)[0]) + ((*p1)[1] - (*p2)[1]) * ((*p1)[1] - (*p2)[1]);
}

int distance(vector<int> *p, vector< vector<int>* > &L) {
	size_t l = L.size();
	int d = distance(p, L[0]);
	for (int k = 0; k < l; ++k) {
		int dk = distance(p, L[k]);
		if (dk < d) d = dk;
	}
	return d;
}

int farthest(vector< vector<int> > &points, vector< vector<int>* > &L) {
	size_t p = points.size();
	size_t l = L.size();
	int m = 0;
	int dm = distance(&(points[m]), L);
	for (size_t j = 1; j < p; ++j) {
		int dj = distance(&(points[j]), L);
		if (dm < dj) {
			m = j;
		}
	}
	return m;
}

void update_neighbours(vector< vector<int> > &points, vector< vector< vector<int>* > > neighbours, vector< vector<int>* > &L, vector<int> *last) {
	size_t n = points.size();
	for (int i = 0; i < n; ++i) {
		vector<int> **nearest1 = &(neighbours[i][0]);
		vector<int> **nearest2 = &(neighbours[i][1]);
		int d1 = distance(&(points[i]), *nearest1);
		int d2 = distance(&(points[i]), *nearest2);
		vector<int> **farthest = (d1 > d2 ? nearest1 : nearest2);
		int d = distance(&(points[i]), *farthest);
		if (d > distance(&(points[i]), last)) {
			*farthest = last;
		}
	}
}

void reconstruction(vector< vector<int> > &points, ofstream &ofile) {
	size_t n = points.size();
	vector< vector<int>* > L;
	L.push_back(&(points[0]));
	vector< vector< vector<int>* > > neighbours(n, {2, L[0]});
	vector< vector< vector<int>* > > CWL;
	vector<int> *first = L[0];
	size_t i = 1;
	while (i < n) {
		int j = farthest(points, L);
		vector<int> *cur = &(points[j]);
		L.push_back(cur);
		update_neighbours(points, neighbours, L, cur);

		if (i == 1) {
			CWL.push_back({L[0], cur});
			continue;
		}
		else if (i == 2) {
			CWL.push_back({L[0], cur});
			CWL.push_back({L[1], cur});
			continue;
		}

		ofile << "Reconstruction n. " << i << " : " << endl;
		vector<int> *nearest1 = neighbours[j][0];
		vector<int> *nearest2 = neighbours[j][1];

		CWL.push_back({nearest1, cur});
		CWL.push_back({nearest2, cur});
		size_t m = CWL.size();
		for (size_t k = 0; k < m; ++k) {
			if ((CWL[k][0] == nearest1 or CWL[k][0] == nearest2) and (CWL[k][1] == nearest1 or CWL[k][1] == nearest2)) {
				CWL[k] = vector< vector<int>* > {NULL, NULL};
			}
			else if (CWL[k][0] != NULL) {
				ofile << (*(CWL[k][0]))[0] << " , " << (*(CWL[k][0]))[1]
					  << " ; "
					  << (*(CWL[k][1]))[0] << " , " << (*(CWL[k][1]))[1] << endl;
			}
		}
	}
}

int main(int argc, char **argv) {
	if (argc <= 1) {
		cout << "You need to specify a file";
	}
	if (argc > 2) {
		cout << "There is only one argument to be specified";
	}
	ifstream ifile(argv[1], ios::in);

	string name = argv[1];
	name = remove_extension(name);
	name = name + (string)".cplx";
	ofstream ofile(name.c_str(), ios::out | ios::trunc);

	string dim;
	getline(ifile, dim);
	if (dim != "2") {
		cout << "This algorithm does only work for dimension 2";
	}

	vector< vector<int> > points;
	size_t i = 0;
	while (!ifile.eof()) {
        vector<int> point(2, 0);
		points.push_back(point);
		int x, y;
		ifile >> x;
		ifile >> y;
        points[i][0] = x;
        points[i][1] = y;
		i++;
	}
	reconstruction(points, ofile);
	return 0;
}
Any idea ?
This algorithm is meant to reconstruct manifolds of R^2 from a discrete set of points.
The vector "points" is likely to contain thousands of points so I've thought that it was better to use pointers not to use to much memory (as "L" and "CWL" contain information from "points")
The code compiles cleanly with a few warnings, all of which, except one, are trivial to fix.
http://coliru.stacked-crooked.com/a/fe39f9dfd3591c3d

This may be a logical error:
main.cpp:42:124: unused parameter 'L' [-Wunused-parameter]
void update_neighbours(vector< vector<int> > &points, vector< vector< vector<int>* > > neighbours, vector< vector<int>* > &L, vector<int> *last) {



> I still have the error from stl_vector.h...

If you are unwilling to state what the error diagnostic says, there is really nothing that can be said other than that the code, as posted, appears to compiles without errors.
Topic archived. No new replies allowed.