0

I am trying initializing sparseMatrix of Eigen, but it does not work when the initialization is in a class description. In the case of initialization in a function, not in a class, it works. I am writting codes by C++ and using Visual Studio 2017. I added public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW , but the problem remains.

#include <iostream>
#include <vector>
#include <Eigen/SparseCore>
#include <Eigen/Sparse>
#include "pch.h"

using namespace Eigen;
namespace A {
    class A 
    {
        std::size_t max_doc_id = 4;
        std::size_t max_term_id = 4;
        SparseMatrix<float, Eigen::RowMajor, int64_t> smat(max_term_id, max_doc_id); 
    public:
        EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    };

I want to decide the size of smat matrix (col=4, row=4) , but the error message is like this (actually it is written in Japanese so it may not correct) "the member A::A::max_term_id is not the name of the type." I appreciate if you can help me.

  • 2
    As a side note, no need for `EIGEN_MAKE_ALIGNED_OPERATOR_NEW` in the case of `SparseMatrix`. – ggael Aug 18 '19 at 12:49

1 Answers1

2

The compiler thinks that you are declaring a member function not a member variable (see here for more info on initialization). The following compiles. I am using Index instead of size_t to get rid of some warnings (narrowing conversion). You can play around with the code here: https://godbolt.org/z/yV1NUL

#include <iostream>
#include <vector>
#include <Eigen/SparseCore>
#include <Eigen/Sparse>

using namespace Eigen;
namespace A {
    class A 
    {
        Index max_doc_id = 4;
        Index max_term_id = 4;
        SparseMatrix<float, Eigen::RowMajor, int64_t> smat{max_term_id, max_doc_id}; 
    public:
        EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    };

}

Note: you should not use using namespace in header files, see "using namespace" in c++ headers

Edit: please also consider what user @ggael says in the comments, most likely you do not need EIGEN_MAKE_ALIGNED_OPERATOR_NEW because the SparseMatrix is not fixed-size vectorizable