Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: passing ‘const MyDiagonalTen’ as ‘this’ argument discards qualifiers #135

Closed
alexpghayes opened this issue Mar 2, 2022 · 2 comments · Fixed by #137
Closed

Comments

@alexpghayes
Copy link
Contributor

I'm having trouble getting the following example to work (although the first two examples in the README work fine for me). Very new to C++ so could be a bug or I could be missing something obvious.

#include <Eigen/Core>
#include <Spectra/SymEigsSolver.h>
#include <iostream>

using namespace Spectra;

// M = diag(1, 2, ..., 10)
class MyDiagonalTen
{
public:
    using Scalar = double;  // A typedef named "Scalar" is required
    int rows() { return 10; }
    int cols() { return 10; }
    // y_out = M * x_in
    void perform_op(const double *x_in, double *y_out) const
    {
        for (int i = 0; i < rows(); i++)
        {
            y_out[i] = x_in[i] * (i + 1);
        }
    }
};

int main()
{
    MyDiagonalTen op;
    SymEigsSolver<MyDiagonalTen> eigs(op, 3, 6);
    eigs.init();
    eigs.compute(SortRule::LargestAlge);
    if (eigs.info() == CompInfo::Successful)
    {
        Eigen::VectorXd evalues = eigs.eigenvalues();
        std::cout << "Eigenvalues found:\n"
                  << evalues << std::endl;
    }

    return 0;
}

I see:

g++ tmp.cpp -o tmp && "/dabox/hayes/spectra/examples/"tmp
tmp.cpp: In member function ‘void MyDiagonalTen::perform_op(const double*, double*) const’:
tmp.cpp:17:34: error: passing ‘const MyDiagonalTen’ as ‘this’ argument discards qualifiers [-fpermissive]
   17 |         for (int i = 0; i < rows(); i++)
      |                                  ^
tmp.cpp:12:9: note:   in call to ‘int MyDiagonalTen::rows()’
   12 |     int rows() { return 10; }
      |         ^~~~
In file included from /usr/local/include/Spectra/SymEigsBase.h:22,
                 from /usr/local/include/Spectra/SymEigsSolver.h:12,
                 from tmp.cpp:2:
/usr/local/include/Spectra/MatOp/internal/ArnoldiOp.h: In instantiation of ‘Spectra::ArnoldiOp<Scalar, OpType, Spectra::IdentityBOp>::Index Spectra::ArnoldiOp<Scalar, OpType, Spectra::IdentityBOp>::rows() const [with Scalar = double; OpType = MyDiagonalTen; Spectra::ArnoldiOp<Scalar, OpType, Spectra::IdentityBOp>::Index = long int]’:
/usr/local/include/Spectra/LinAlg/Arnoldi.h:119:59:   required from ‘Spectra::Arnoldi<Scalar, ArnoldiOpType>::Arnoldi(ArnoldiOpType&&, Spectra::Arnoldi<Scalar, ArnoldiOpType>::Index) [with Scalar = double; ArnoldiOpType = Spectra::ArnoldiOp<double, MyDiagonalTen, Spectra::IdentityBOp>; Spectra::Arnoldi<Scalar, ArnoldiOpType>::Index = long int]’
/usr/local/include/Spectra/LinAlg/Lanczos.h:52:62:   required from ‘Spectra::Lanczos<Scalar, ArnoldiOpType>::Lanczos(T&&, Spectra::Lanczos<Scalar, ArnoldiOpType>::Index) [with T = Spectra::ArnoldiOp<double, MyDiagonalTen, Spectra::IdentityBOp>; Scalar = double; ArnoldiOpType = Spectra::ArnoldiOp<double, MyDiagonalTen, Spectra::IdentityBOp>; Spectra::Lanczos<Scalar, ArnoldiOpType>::Index = long int]’
/usr/local/include/Spectra/SymEigsBase.h:237:37:   required from ‘Spectra::SymEigsBase<OpType, BOpType>::SymEigsBase(OpType&, const BOpType&, Spectra::SymEigsBase<OpType, BOpType>::Index, Spectra::SymEigsBase<OpType, BOpType>::Index) [with OpType = MyDiagonalTen; BOpType = Spectra::IdentityBOp; Spectra::SymEigsBase<OpType, BOpType>::Index = long int]’
/usr/local/include/Spectra/SymEigsSolver.h:158:69:   required from ‘Spectra::SymEigsSolver<OpType>::SymEigsSolver(OpType&, Spectra::SymEigsSolver<OpType>::Index, Spectra::SymEigsSolver<OpType>::Index) [with OpType = MyDiagonalTen; Spectra::SymEigsSolver<OpType>::Index = long int]’
tmp.cpp:27:47:   required from here
/usr/local/include/Spectra/MatOp/internal/ArnoldiOp.h:120:50: error: passing ‘const MyDiagonalTen’ as ‘this’ argument discards qualifiers [-fpermissive]
  120 |     inline Index rows() const { return m_op.rows(); }
      |                                                  ^
tmp.cpp:12:9: note:   in call to ‘int MyDiagonalTen::rows()’
   12 |     int rows() { return 10; }
      |         ^~~~

[Done] exited with code=1 in 2.437 seconds
@alexpghayes alexpghayes changed the title Custom matrix op errors error: passing ‘const MyDiagonalTen’ as ‘this’ argument discards qualifiers Mar 2, 2022
@yixuan
Copy link
Owner

yixuan commented Mar 4, 2022

Hi Alex, can you try to simply add a const keyword to the rows() and cols() member functions? I will fix the example later.

@alexpghayes
Copy link
Contributor Author

Thanks, fixed it for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants