1

I use Eigen library in my C++ project and I don't understand how to pass an Eigen::Vector in parameter of my function.

I have in my main function :

Eigen::Vector<double, 3> direction_vector_ned = {1.0, 2.0, 3.0};

Eigen::Vector<double, 3> direction_vector_lla = Reperes::ned_to_lla(direction_vector_ned);

In my Reperes class :

class Reperes
{
  private:

  public:
    static Eigen::Vector<double, 3> ned_to_lla(const Eigen::Vector<double, 3>& ned);
};

And the implementation match with the prototype :

static Eigen::Vector<double, 3> ned_to_lla(const Eigen::Vector<double, 3>& ned)
{
    Eigen::Vector<double, 3> lla = {0.0, 0.0, 0.0};
    ...
    return lla;
}

I think the architecture is correct but the compiler raise an error at the call of ned_to_lla() in my main.

I got this error :

undefined reference to `Reperes::ned_to_lla(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)

I explicitly declared my direction_vector_ned variable as a Vector<double, 3>. Why it has been casted into a Matrix<double, 3, 1, 0, 3, 1> ?

I tried to use EigenBase type as mentionned in this section https://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html but it didn't work.

3
  • 2
    Matrix has default parameter, so Eigen::Vector<double, 3> IS Matrix<double, 3, 1, 0, 3, 1> Commented Jun 9 at 18:02
  • 2
    "undefined reference to Reperes::ned_to_lla" You define the free function ::ned_to_lla Commented Jun 9 at 18:03
  • You're right. I replaced the parameter with an int and got the same error. But I put the #include ‘reperes.hpp’ in my main.cpp Commented Jun 9 at 18:26

1 Answer 1

3

To expand on @Jarod42's comment, you never actually define Reperes::ned_to_lla. Instead, you define a free function that happens to have the same name as a member function of your Reperes class. This is NOT an implementation of Reperes::ned_to_lla:

static Eigen::Vector<double, 3> ned_to_lla(const Eigen::Vector<double, 3>& ned)
{
    Eigen::Vector<double, 3> lla = {0.0, 0.0, 0.0};
    ...
    return lla;
}

The above function does not belong to any class.

What you should have written was this:

Eigen::Vector<double, 3> Reperes::ned_to_lla(const Eigen::Vector<double, 3>& ned)
{
    Eigen::Vector<double, 3> lla = {0.0, 0.0, 0.0};
    ...
    return lla;
}

Notice the Reperes:: in front of the function name and no keyword static either (HT @gerum). That's what identifies it as part of your Reperes class and not some free function.

Sign up to request clarification or add additional context in comments.

8 Comments

If I do this, i got this error: cannot declare member function 'static Eigen::Vector<double, 3> Reperes::ned_to_lla(Eigen::Vector<double, 3>&)' to have static linkage
Does member function Reperes::ned_to_lla contain member variables of the Reperes class? Then it cannot be static.
No, it doesn't.
@jjramsey The definition of the function should not contain the static keyword.
Yes, i resolved the problem like that. I thought the function prototypes had to match perfectly
@gerum Good point. I'll update my post accordingly.
@LnlB "I removed the static keyword in the .cpp file but i left it in the hpp" That's how you're supposed to do it: stackoverflow.com/a/5373143/1643973
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.