【发布时间】:2014-02-25 04:32:14
【问题描述】:
我是一名物理学家,在类编程方面没有太多经验。如果有人可以提供帮助,我将不胜感激。我已经在 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;
}
当我尝试编译这个时,我得到了一堆错误。
----@----:~/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
我确定我在这里遗漏了一些关键方面;有人请指出。
谢谢!
【问题讨论】: