我是一位物理学家,在课程编程方面没有多少经验。如果有人能为此提供帮助,我将不胜感激。我已经成功地在python类中使用了numpy数组,但是在这里丢失了。在课堂上使用犰狳矩阵
动机很简单。我需要使用一个具有几个矩阵的类作为私有成员并对它们执行一些操作。看看下面的内容。
#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;
}
这编译并正确运行。但是,如果我尝试使用犰狳矩阵,事情不起作用。
#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;
}
当我尝试编译,我得到了一堆或错误。
[email protected]:~/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
我相信我在这里错过了一些关键方面;有人请指出。
谢谢。
Armadillo矩阵类可以选择使用编译时的大小 - 它们被称为固定大小的矩阵。例如'mat :: fixed <2,2> A;'请参阅文档中的[高级构造函数](http://arma.sourceforge.net/docs.html#adv_constructors_mat)部分。同样,向量也可以有一个固定大小的选项:'vec :: fixed <10> v;' – mtall
好点,会编辑 –