读书人

静态数组存储数据遇到有关问题

发布时间: 2012-03-09 16:54:58 作者: rapoo

静态数组存储数据遇到问题
我的目的:将类Element中的矩阵数组K[4][4] 、N[4][4]、P[4]分别合成到一起,总装成一个大型的矩阵数组,但是在执行的过程中,却碰到了一个比较严峻的的问题--静态数组大小似乎有限制,有一个最高值,一旦超过了,就会报错,同时由于strLength是和Elementvec的元素个数有关的,因此在动态变化。因此我想要分别利用一个动态数组来存储K4][4] 、N[4][4]、P[4]。但是我不会做,还请那位大神帮忙啊,小弟不甚感激!!!!!!
typedef vector<Element>ElementVec;//其中Element是一个类,并且之前已经初始化了,里面存储了294个元素。
void EleKMatrixAssembly(ElementVec &Elementvec)
{
long int strLength;//定义单元刚度矩阵的大小
strLength = long(Elementvec.size() * 4);

float KMatrixAssembly[strLength][strLength];//定义总刚度矩阵KMatrixAssembly,并初始化为0。
float NMatrixAssembly[strLength][strLength];//定义总形函数矩阵NMatrixAssembly,并初始化为0。
float PMatrixAssembly[1][strLength];//定义总右端项PMatrixAssembly,并初始化为0。

for (int i = 0; i != strLength; i++ )
{
for(int j = 0; j != strLength; j++)
{
KMatrixAssembly[i][j] = 0;
NMatrixAssembly[i][j] = 0;
}
PMatrixAssembly[0][i] = 0;
}

for (int i = 0; i != Elementvec.size(); i++)
{
int NodeNum[4];

for (int j = 0; j !=4; j++)
{
NodeNum[j] = Elementvec[i].m_nNodeNum[j];
//记录单元刚度矩阵的行号和列号,并将其赋值给对应的总刚矩阵的行号与列号。
}
for (int j = 0; j !=4; j++)
{
for(int k = 0; k != 4; k++)
{
KMatrixAssembly[NodeNum[j] - 1][NodeNum[k] - 1] += Elementvec[i].K[j][k];
NMatrixAssembly[NodeNum[j] - 1][NodeNum[k] - 1] += Elementvec[i].N[j][k];
}

PMatrixAssembly[0][NodeNum[j] - 1] += Elementvec[i].P[j];
}
}
}


1>------ 已启动生成: 项目: simulate, 配置: Debug Win32 ------
1>Compiling...
1>main.cpp
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(266) : error C2057: expected constant expression
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(266) : error C2466: cannot allocate an array of constant size 0
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(266) : error C2057: expected constant expression
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(266) : error C2466: cannot allocate an array of constant size 0
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(266) : error C2087: 'KMatrixAssembly' : missing subscript
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(266) : error C2133: 'KMatrixAssembly' : unknown size
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(267) : error C2057: expected constant expression
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(267) : error C2466: cannot allocate an array of constant size 0
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(267) : error C2057: expected constant expression
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(267) : error C2466: cannot allocate an array of constant size 0
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(267) : error C2087: 'NMatrixAssembly' : missing subscript
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(267) : error C2133: 'NMatrixAssembly' : unknown size
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(268) : error C2057: expected constant expression
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(268) : error C2466: cannot allocate an array of constant size 0
1>l:\王海涛文件夹\2012\simulate\simulate\main.cpp(268) : error C2087: 'PMatrixAssembly' : missing subscript
1>Build log was saved at "file://l:\王海涛文件夹\2012\simulate\simulate\Debug\BuildLog.htm"
1>simulate - 15 error(s), 0 warning(s)

[解决办法]
int EleKMatrixAssembly(ElementVec &Elementvec,int ArrayLength)
//ArrayLength定义为总装单元刚度矩阵的大小
{
vector <vector <float> >KMatrixAssembly(ArrayLength);//定义总刚度矩阵KMatrixAssembly

vector <vector <float> >NMatrixAssembly(ArrayLength);//定义总刚度矩阵KMatrixAssembly



vector <vector <float> > PMatrixAssembly(1);//定义总右端项PMatrixAssembly

vector <vector <float> >KAndNMatrixAssembly(2 * ArrayLength);

//定义各个矩阵的大小
for (int i = 0; i != ArrayLength; i++)
{
KMatrixAssembly[i].resize(ArrayLength);//K和N定义为ArrayLength * ArrayLength 的矩阵
NMatrixAssembly[i].resize(ArrayLength);
}
PMatrixAssembly[0].resize(ArrayLength);//P定义为 1 * ArrayLength的矩阵
for (int i = 0; i != 2 * ArrayLength; i++)
{
KAndNMatrixAssembly[i].resize(2 * ArrayLength);
//KN定义为2ArrayLength * 2ArrayLength 的矩阵
}

//将各个矩阵初始化为0
for (int i = 0; i != ArrayLength; i++ )
{
for(int j = 0; j != ArrayLength; j++)
{
KMatrixAssembly[i][j] = 0;
NMatrixAssembly[i][j] = 0;

}
PMatrixAssembly[0][i] = 0;
}
for (int i = 0; i != 2 * ArrayLength; i++)
{
for (int j = 0; j != 2 * ArrayLength; j++)
{
KAndNMatrixAssembly[i][j] = 0;
}
}

//单元矩阵总装
for (int i = 0; i != Elementvec.size(); i++)
{
int NodeNum[4];

for (int j = 0; j !=4; j++)
{
NodeNum[j] = Elementvec[i].m_nNodeNum[j];
//记录单元刚度矩阵的行号和列号,并将其赋值给对应的总刚矩阵的行号与列号。
}
for (int j = 0; j !=4; j++)
{
for(int k = 0; k != 4; k++)
{
KMatrixAssembly[NodeNum[j] - 1][NodeNum[k] - 1] += Elementvec[i].K[j][k];
NMatrixAssembly[NodeNum[j] - 1][NodeNum[k] - 1] += Elementvec[i].N[j][k];
}
PMatrixAssembly[0][NodeNum[j] - 1] += Elementvec[i].P[j];
}
}
for (int i = 0; i != ArrayLength; i ++)
{
for (int j = 0; j != ArrayLength; j++)
{
KAndNMatrixAssembly[i][j] = KMatrixAssembly[i][j];
KAndNMatrixAssembly[i + ArrayLength][j + ArrayLength] = NMatrixAssembly[i][j];
}
}
return 0;
}

读书人网 >C++

热点推荐