Using Armadillo Matrices Inside a Class

I am a physicist with little programming experience with classes. I would be grateful if someone would help in this. I have successfully used numpy arrays inside python classes, but here I lost.

The motivation is simple. I need to use a class with several matrices as private members and perform some operations on them. Take a look at the following.

#include<iostream>
#include<armadillo>

using namespace std;

class myclass{
    // a matrix
    double A[2][2];
public:
    int set_element(double);
};

int main(){
    myclass x;
    x.set_element(2.0);
}

int myclass::set_element(double num){
    // a function to assign a value to the first array element.
    A[0][0] = num;
    cout << A[0][0] << endl;
    return 0;
}

It compiles and runs correctly. But if I try to use the armadillo matrix, it won’t work out.

#include<iostream>
#include<armadillo>

using namespace std;
using namespace arma;

class myclass{
private:
    // a matrix
    mat A(2,2);
public:
    int set_element(double);
};

int main(){
    myclass x;
    x.set_element(2.0);
}

int myclass::set_element(double num){
    //function to set the first element.
    A(0,0) = num;
    cout << A(0,0) << endl;
    return 0;
}

When I try to compile this, I get a bunch or errors.

jayasurya@yvjs:~/comp/cpp$ g++ dummy.cpp -larmadillo
dummy.cpp:10:15: error: expected identifier before numeric constant
dummy.cpp:10:15: error: expected ‘,’ or ‘...’ before numeric constant
dummy.cpp: In member function ‘int myclass::set_element(double)’:
dummy.cpp:22:14: error: no matching function for call to ‘myclass::A(int, int)’
dummy.cpp:22:14: note: candidate is:
dummy.cpp:10:13: note: arma::mat myclass::A(int)
dummy.cpp:10:13: note:   candidate expects 1 argument, 2 provided
dummy.cpp:23:22: error: no matching function for call to ‘myclass::A(int, int)’
dummy.cpp:23:22: note: candidate is:
dummy.cpp:10:13: note: arma::mat myclass::A(int)
dummy.cpp:10:13: note:   candidate expects 1 argument, 2 provided

I am sure that some key aspect is missing; someone please indicate this.

Thanks.

+4
source share
2 answers

armadillo , , . armadillo , , .

class myclass{
private:
    // a matrix - DECLARATION of a member variable
    mat A;
public:
    myclass() // Constructor
    : A(2, 2) { // Default matrix member variable initialization
    }

    // another constructor where you can supply other dimensions:
    myclass(int rows, int cols)
    : A(rows, cols) { // matrix member variable initialization
    } 

    int set_element(double);
};

, ++ 11, , , , :

class myclass {
private:
    // a matrix - this syntax allows you to specify the default initialization parameters for this variable
    mat A {2, 2}; 
public:
    int set_element(double);
};

, , mtall , :

class myclass {
private:
    // a matrix - this syntax allows you to specify a compile-time size
    mat::fixed<2, 2> A; 
public:
    int set_element(double);
};
+3

Martin J., , , .

. , ++ (, gcc). , , . (.. 10x10 100 ) .

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

class myclass{
private:
   // a fixed-size matrix: allows more optimization
   // (generally recommended only for sizes <= 10x10)
   mat::fixed<2,2> A;
public:
   int set_element(double);
};

int main(){
  myclass x;
  x.set_element(2.0);
}

int myclass::set_element(double num){
  //function to set the first element.
  A(0,0) = num;
  cout << A(0,0) << endl;
  return 0;
}
+1

All Articles