0

I am working on an existing code adding some features. A class arc exists with start_point, end_point, and center_point. This is how start point is obtained for example:

/** returns start turn point */
const Formulas::LLPoint& get_start_turn_point() const
{
  return start_point;
}

And its longitude is logged like this:

act_msg.startLongitude = flyBy.get_start_turn_point().longitude;
DEBUG_LOG1 (LVL1, "Testing, start longitude of turn:");
DEBUG_LOG1 (LVL1, flyBy.get_start_turn_point().longitude);

This logs the longitude perfectly fine. Now I want to add to the class a vector of points. That will require some basic trigonometry but the problem for me is working with vectors in C++ (this is my first experience coding in this language). I tried with just two points for testing:

/** returns list of points */
std::vector<Formulas::LLPoint>& get_arc_points()
{
  arc_points.push_back(start_point);
  arc_points.push_back(end_point);
  return arc_points;
}

I want to log the same longitude as before, but this is not working:

DEBUG_LOG1 (LVL1, "Testing, same number from vector:");
DEBUG_LOG1 (LVL1, flyBy.get_arc_points()[0].longitude);

It is not even compiling, with the following error:

error: passing ‘const FVIS::ArcDataSegment’ as ‘this’ argument discards qualifiers [-fpermissive] DEBUG_LOG1 (LVL1, flyBy.get_arc_points()[0].longitude);

Where is the problem? Any help is appreciated, thank you.

Daniel Viaño
  • 485
  • 1
  • 9
  • 26
  • 1
    `get_arc_points` is a function, so you need to call it with parentheses -> `get_arc_points[0]` should be `get_arc_points()[0]` – UnholySheep Oct 24 '17 at 07:48
  • Thank you, now the problem is other: error: passing ‘const FVIS::ArcDataSegment’ as ‘this’ argument discards qualifiers [-fpermissive] DEBUG_LOG1 (LVL1, flyBy.get_arc_points()[0].longitude); – Daniel Viaño Oct 24 '17 at 07:53
  • 1
    this new error requires more code to be sure, but I'm guessing that `flyBy` is marked `const` and the `get_arc_points` function is missing the `const` (that you have on `get_start_turn_point()`). Change the function signature: `std::vector& get_arc_points()` to `const std::vector& get_arc_points() const` (or create an overload) – UnholySheep Oct 24 '17 at 07:59
  • But if I put const in the get_arc_points() function I cannot push_back the two points to the vector, I get the same problem in that part: error: passing ‘const std::vector’ as ‘this’ argument discards qualifiers [-fpermissive] arc_points.push_back(start_point); What do you mean by creating an overload? I am very begginer and working with things I don't really understand, sorry. – Daniel Viaño Oct 24 '17 at 08:09
  • 3
    So, why is a function that is called `get_XXX` modifying a member variable? That is not a good idea (as it does not do what the name implies). And since you're a beginner you should probably refer to [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first (as SO is not a tutorial site) – UnholySheep Oct 24 '17 at 08:11
  • You probably want to create a local vector inside get_arc_points(), add the points to it and return it. However, as you haven't posted the implementation of arc, it's impossible to say anything definite. – Knoep Oct 24 '17 at 12:09
  • Agree with @UnholySheep -- this error occurs when your code violates const-ness. – Eli Oct 24 '17 at 13:19

0 Answers0