c++ - 无法将 Eigen 对象从 main 传递到附加类中的函数

标签 c++ eigen

我在 main 中,并尝试将 Eigen 对象传递给位于不同类中的函数。当两个函数位于同一个文件中时,我可以毫无问题地进行此函数调用。请参阅我的示例代码来解决我的问题。它不链接。有什么问题,如何解决?

Eigen 的创建者 eigen.tuxfamily.org 谈论如何将 eigen 对象传递给函数。不幸的是,我无法将他们的示例扩展到单个文件之外。

如何传递对象:

http://eigen.tuxfamily.org/api/TopicPassingByValue.html

如何使用表达式模板传递对象:

http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html

main.cpp

#include <cstdlib>
#include <iostream>
#include <Eigen>
#include "MatrixMathTester.h"
using namespace std;
using namespace Eigen;

int main(int argc, char** argv)
{
    MatrixMathTester matrixMathObject;

    MatrixXd outputMatrix(2, 2);
    outputMatrix << 22, 11, 22, 11;

    matrixMathObject.internalMatrixMath(outputMatrix);

    return 0;
}

MatrixMathTester.h:

#ifndef MATRIXMATHTESTER_H
#define MATRIXMATHTESTER_H

#include <iostream>
#include <Eigen>
using namespace Eigen;

class MatrixMathTester {
public:
    MatrixMathTester();
    MatrixMathTester(const MatrixMathTester& orig);
    virtual ~MatrixMathTester(); 

    template <typename Derived>
    void internalMatrixMath(const MatrixBase<Derived>& inputMatrix);
};
#endif /* MATRIXMATHTESTER_H */

MatrixMathTester.cpp:

#include "MatrixMathTester.h"
using namespace std;
using namespace Eigen;

MatrixMathTester::MatrixMathTester(){}
MatrixMathTester::MatrixMathTester(const MatrixMathTester& orig){}
MatrixMathTester::~MatrixMathTester(){}

template <typename Derived>
void MatrixMathTester::internalMatrixMath(const MatrixBase<Derived>& inputMatrix)
{
cout << "InputMatrix" << endl << inputMatrix << endl;
}

错误

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/testinggrounds
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++ -I/Users/folder/Documents/CPP_Librarys/eigen/Eigen   -c -g \
    -I../../../CPP_Library/eigen/Eigen -MMD -MP -MF 
    build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/MatrixMathTester.o.d
g++ -I/Users/folder/Documents/CPP_Librarys/eigen/Eigen   -c -g -MMD -MP \
    -MF build/Debug/GNU-MacOSX/MatrixMathTester.o.d -o \
    build/Debug/GNU-MacOSX/MatrixMathTester.o MatrixMathTester.cpp
mkdir -p dist/Debug/GNU-MacOSX
g++ -I/Users/folder/Documents/CPP_Librarys/eigen/Eigen    -o \
    dist/Debug/GNU-MacOSX/testinggrounds build/Debug/GNU-MacOSX/main.o \
    build/Debug/GNU-MacOSX/MatrixMathTester.o  
Undefined symbols:
  "void MatrixMathTester::internalMatrixMath<Eigen::Matrix<double, -1, -1, 0, -1, -1>
   >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&)",
referenced from:
  _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

最佳答案

不幸的是,您无法将模板成员函数的定义放入 MatrixMathTester.cpp 中。当编译器看到该文件时,它不知道您需要什么实例化,因此它不会生成您需要的实例。

如果您将定义移至 MatrixMathTester.h 那么一切都会好起来的。编译 main.cpp 时,编译器将看到模板的主体和您需要的实例化,并执行正确的操作。

关于c++ - 无法将 Eigen 对象从 main 传递到附加类中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9551479/

相关文章:

c++ - std::string 获取 (char *) 而不是 (const char *)

c++ - Visual Studio 2019 中的修改和静态/动态绑定(bind)

c++ - 如何在 C++ 中读取 FORTRAN 格式的数字

c++ - 如何从原始指针获取 shared_ptr?

c++ - 避免并发访问变量

c++ - 矩阵库是否有顺序中性?

c++ - 模板库的编译器内存消耗(boost + Eigen)

c++ - 如何禁止在 Windows MFC 应用程序中创建 .pf(预取)文件?

c++ - 如何集成使用表达式模板的库?

c++ - CMakeLists.txt文件中的config Eigen