Need help translating a snippet of AprilTags Java to C++

I am trying to get a C++ example program for AprilTags stamped out quickly (trying to get examples into wpilib in time for the next version or so).

I have this in java:

      AprilTagDetection[] detections = detector.detect(grayMat);
      for (AprilTagDetection detection : detections) {
        // process detection here

I am having trouble writing the code to iterate over the detections in C++:

      cv::Size s = grayMat.size();
      frc::AprilTagDetector::Results detections = detector.Detect (s.width, s.height,;

      // dumb Java guy doesn't know how to iterate over detections, this ain't right
      for (frc::AprilTagDetection detection : detections) {
        // process detection

I haven’t looked at C++ for a long time; can someone help me get over this hurdle?

So, I haven’t actually tried this out (nor am I particularly knowledgeable in C++), but docs say

Each array element is a pointer to an AprilTagDetection.

In that case, I’d try this to dereference said pointer:

      for (const frc::AprilTagDetection& detection : detections) {
        detection.GetId(); // Or whatever you want
1 Like

This might end up making each iteration have a const reference to the pointer, rather than derefencing, but don’t quote me on that. I’m fairly comfortable in c++ but I don’t pretend to understand the magic of for each loops.

It’s actually a pointer, so you need:

for (const frc::AprilTagDetection* detection : detections) {
  // process detection; you’ll need to use detection->

Thanks. It works. Hard to make the shift from the Java “(almost-)everything is a reference” to the C++ “I need to think about which is which”. Especially since it’s been about 25 years since I’ve tried to learn C++!

Turns out I thought references were more or less syntactic sugar over dereferencing pointers, and I was going to post what you did, but figured it’d be more “elegant” if I did it with references. Turns out they aren’t, in a few ways. The more you know, i guess.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.