c++ - 将特征库中的 VectorXi 传递给类方法

标签 c++ compiler-errors eigen

下面的主文件应该将一个 VectorXi 传递给我的名为 Test 的类,然后该类有一个对其执行某些操作的方法(为了演示,它只打印所有元素的总和):

#include <iostream>
#include <eigen3/Eigen/Dense>
#include "test.h"

using namespace std;
using namespace Eigen;

int main(int argc, const char * argv[]) {

    VectorXi m(3);
    m[0] = 1;
    m[1] = 2;
    m[2] = 6;

    Test test;
    test.mySum(m);

    return 0;
}

测试.h

#ifndef __CPP_Playground__test__
#define __CPP_Playground__test__

#include <iostream>
#include <eigen3/Eigen/Dense>

using namespace std;
using namespace Eigen;

class Test {
public:
    void mySum(VectorXi vec); // Does not work. 
//    void mySum(VectorXi vec){cout << vec.sum() << endl;}; // Works.
};

#endif /* defined(__CPP_Playground__test__) */

和测试.cpp

#include "test.h"

void mySum(VectorXi vec){
    cout << vec.sum() << endl;
};

在 OS X 10.10.2 上使用 Xcode 6.1.1 编译时出现错误消息:

Undefined symbols for architecture x86_64:
  "Test::mySum(Eigen::Matrix<int, -1, 1, 0, -1, 1>)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

我尝试在项目设置下使用libstdc++代替libc++,但是没有用。我通过 brew install eigen 使用 Homebrew 安装了 eigen 库。为什么它使用直接在 test.h 中定义的方法(参见注释行)而不是在 test.cpp 中定义的方法?

最佳答案

这与 Eigen 无关,您只是省略了类前缀 Test::在 cpp 文件中:

void Test::mySum(VectorXi vec){
    cout << vec.sum() << endl;
}

此外,尾随 ;在适当的 C++ 中不需要,您应该传递 vec通过引用将参数声明为 VectorXi &vec 的对象甚至更好地使用 Eigen::Ref<VectorXi> vec允许通过引用传递兼容的对象。

关于c++ - 将特征库中的 VectorXi 传递给类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407634/

相关文章:

c++ - GetFileAttributesA 为现有目录返回 "17"。 "16"表示它是一个目录,文档中没有提到 "17"

c - 编译C代码时出现错误C2113

c - 表达式必须有一个常量值(C 中的错误)

c++ - 如何在 Eigen 中定义 VectorXd 数组

c++ - MessageBox "Abnormal program termination"让我的应用程序保持运行

c++ - 悬空引用和未定义行为

java - Eclipse : The type VertexNotFoundException could not be resolved. It is indirectly referenced from required .class files

c++ - 如何正确地从nalgebra::Matrix转换为Eigen::MatrixXf?

c++ - 如何在 Eigen 中使用不完全 LU 分解?

c++ - C++中文件的输入/输出