[英]Calling a Matrix from one class to other / main
我是C ++的新手,正在为自己的项目开发电源系统仿真算法。
到目前为止,对于潮流算法,我获得了很好的结果。 我试图通过创建许多类来优化计算。
但是,对于每个变量xxx
(int,double,数组/向量),我都可以使用Class.xxx
在类之外调用它,除了矩阵类型( vector<vector<complex<double>>>
)。 这是代码的一部分:
class Load_Flow_Class {
public:
vector<vector<complex<double>>> Ybus; // this is a complexe matrix
vector<complex<double>> LPQIK;
vector<complex<double>> LPQKI;
complex<double> TotalLineLoses;
int slackbus;
//....... initiate matrix and calculate values.....................
void YBIKmatrix (int nbus) {
vector<vector<complex<double>>> Ybus(nbus);
for (int i = 0; i < nbus; i++) {
Ybus[i].resize(nbus);
}
for (int i = 0; i < nbus; i++) {
for (int j = 0; j < nbus; j++) {
Ybus[i][j]=/*.....*/
}
}
}
cout << LF.Ybus[i][j]; //-------------> no issue
//.................from main()......................
nbus = 5; //example
Load_Flow_Class LF;
LF.YBIKmatrix(nbus);
for (int i /*....*/) {
for (int j /*....*/) {
cout << LF.Ybus[i][j];
//----> no display and cause the program to stop
}
}
这仅在矩阵类型( Ybus
)上发生
在编辑您的问题以使其更清楚时,我特别注意了两行。 类定义中的这一行:
vector<vector<complex<double>>> Ybus; // this is a complexe matrix
YBIKmatrix
方法中的这一行:
vector<vector<complex<double>>> Ybus(nbus);
因为您在成员函数中再次声明了变量,所以它使类成员Ybus
阴影 。 函数中Ybus
发生的所有更改仅发生在局部变量上,而不发生在类变量上。
要解决此问题,请在成员函数中删除此行:
vector<vector<complex<double>>> Ybus(nbus);
如果确实要设置向量的大小,则可以改用resize
,而不是尝试再次构造它。
Ybus.resize(nbus);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.