Compilator error

I am a bit lost. I have to make an exercise, and the test probes return:


====== Sortie de g++ (1): ======
/tmp/tmpl2vzw2/test.cc: In function ‘_1aa6692b7e103514443a9d5e8f587e7::Triangle _4f13de2a0832e55e13a4f01e7e070c807d::trois_plus_proches(const _1aa6692b7e103514443a9d5e8f587e7::Point&, const Commune&)’:
/tmp/tmpl2vzw2/test.cc:152:13: error: passing ‘const _1aa6692b7e103514443a9d5e8f587e7::Point’ as ‘this’ argument of ‘_1aa6692b7e103514443a9d5e8f587e7::Point& _1aa6692b7e103514443a9d5e8f587e7::Point::operator=(_1aa6692b7e103514443a9d5e8f587e7::Point&&)’ discards qualifiers [-fpermissive]
   result[0] =  REFERENCE::plus_proche(depart, village);
             ^
compilation terminated due to -Wfatal-errors.

====== Sortie de clang++ (1): ======
/tmp/tmpl2vzw2/test.cc:152:13: fatal error: no viable overloaded '='
  result[0] =  REFERENCE::plus_proche(depart, village);
  ~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/tmpl2vzw2/student.cc:13:8: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const _1aa6692b7e103514443a9d5e8f587e7::Point', but method is not marked const
struct Point {
       ^
/tmp/tmpl2vzw2/student.cc:13:8: note: candidate function (the implicit move assignment operator) not viable: 'this' argument has type 'const _1aa6692b7e103514443a9d5e8f587e7::Point', but method is not marked const
struct Point {
       ^
1 error generated


I don't know what are made the test. My code is:

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
struct Point {
	double x;
	double y;
};

struct Triangle {
	Point a[3];
	const Point& operator[](size_t i) const {return a[i]; }
};

Point plus_proche(Point const& depart, Commune const& village)
{
	Point vuelta = village.front();
	
	double menor = distance(depart, vuelta);
	
	for(size_t i = 1; i < village.size(); ++i){
		
		if( distance(depart, village.at(i)) < menor ) {
			menor = distance(depart, village.at(i));
			vuelta = village.at(i);
		}
	}
	
	return vuelta;
}

Triangle trois_plus_proches(Point const& depart, Commune const& village)
{
	Triangle vuelta;
	
	vector<Estructura> vectorAux;
	
	Estructura auxEstructura;
	
	double auxDistance (0);
	
	for( size_t i = 0; i < village.size(); ++i ) {
		auxDistance = distance(depart, village.at(i));
		
		auxEstructura.dist = auxDistance;
		auxEstructura.punto = village.at(i);
		
		vectorAux.push_back( auxEstructura );
	}
	
	sort( vectorAux );
	
	vuelta.a[0] = vectorAux.at(0).punto;
	vuelta.a[1] = vectorAux.at(1).punto;
	vuelta.a[2] = vectorAux.at(2).punto;
	
	return vuelta;
}


I try to write const struct Point, but the compiler don't let me.
http://www.eelis.net/iso-c++/testcase.xhtml
4. Make sure any line numbers you mention in the testcase are correct
6. Make sure your testcase is self-contained and actually reproduces the problem

It seems that you've got a constant point, and you want to modify it (assign it another point)

Also, perhaps you may also want the non-const version of `operator[]' for Triangle


> _1aa6692b7e103514443a9d5e8f587e7::Triangle
¿is that a namespace?
I haven't made the tests. They are an online tool that correct my exercise, that is why I don't know this code.

How can I correct it to use " the non-const version of `operator[]' for Triangle"? If I don't override the operator Triangle[], my program doesn't compile.
As you just said, override the operator:
1
2
3
4
5
6
struct Triangle {
    Point a[3];

    const Point& operator[](std::size_t index) const { return a[index]; }
          Point& operator[](std::size_t index)       { return a[index]; }
};


Also, @ne555 was referring to the problems with your 'testcase', NOT the tests themselves. As in, the things you are posting here. We want to have something that we can compile and run ourselves, get the same output as you, and then fiddle with to help you fix your problem. Just for future reference.
Last edited on
Topic archived. No new replies allowed.