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{
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[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:
mat A(2,2);
public:
int set_element(double);
};
int main(){
myclass x;
x.set_element(2.0);
}
int myclass::set_element(double num){
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.
source
share