我在对象中遇到了Object数组的问题:
我有一个带有对象数组的类:
class element
{
public:
element() {};
LinePiece* arrayLP;
我在数组中有一个类:
class LinePiece
{
public:
LinePiece() {};
AnsiString Type;
int ElementNr;
int Status;
int X, Y;
所以在Element对象中我有一个LinePiece数组。奇怪的是,当我填写ElementArray [1] .LPArray [0];它会被下一个对象覆盖(ElementArray [2] .LPArray [0];
我用以下代码填写:
String FileBuffer;
String Regel;
String Code;
element* GetElementInfo()
{
//alle onderdelen van een Elementobject
String OrderNumber; //ON
String OrderBrand; //MO
String ElementNumber; //SN
String ElementMarking; //RS
String ReinforcementPattern; //OW
String CalculationNumber; //CN
String ElementLength; //LE
String ElementWidth; //WD
String ElementBeginX; //BP
String ConcreteThickness; //TH
String Isulation; //IH
String Weight; //NW
element *ElementArray = new element[100];
LinePiece *LPArray = new LinePiece[100];
bool firstElement = true;
int Index =0;
int LPIndex = 0;
for(int i = 1; i <= FileBuffer.Length(); i++)
{
if(FileBuffer[i] != 0)
{
if(FileBuffer[i] != IntToStr('\r')) //controleren of je op einde regel zit, zoniet vul de string "regel" aan
{
if(FileBuffer[i] != IntToStr('\r') && FileBuffer[i] != IntToStr('\n'))
{
Regel = Regel + FileBuffer[i];
}
}
else //kijken wat er op de regel staat
{
Code = Regel.SubString(0,2);
if(Code == "ON") //Ordernummer
{
OrderNumber = Regel.SubString(4, (Regel.Length() -3));
Regel = "";
}
if(Code =="MO") //Ordermerk
{
OrderBrand = Regel.SubString(4, (Regel.Length() -3));
Regel = "";
}
if(Code =="SN") //Element nummer
{
ElementNumber = Regel.SubString(4, (Regel.Length()-3));
Regel = "";
}
if(Code =="RS") //Element marking
{
ElementMarking = Regel.SubString(4, (Regel.Length()-3));
Regel = "";
}
if(Code =="CN") //Calculatienummer
{
CalculationNumber = Regel.SubString(4, (Regel.Length()-3));
Regel = "";
}
if(Code == "LE") //Element lengte
{
ElementLength = Regel.SubString(4,(Regel.Length()-3));
Regel = "";
}
if(Code == "WD") //element breedte
{
ElementWidth = Regel.SubString(4,(Regel.Length()-3));
Regel = "";
}
if(Code == "BP") //beginpunt X
{
ElementBeginX = Regel.SubString(4, (Regel.Length()-3));
Regel = "";
}
if (Code == "OW") //Wapeningspatroon
{
ReinforcementPattern = Regel.SubString(4, (Regel.Length()-3));
Regel = "";
}
if(Code == "TH") //Beton dikte
{
ConcreteThickness = Regel.SubString(4,(Regel.Length()-3));
Regel = "";
}
if(Code == "IH") //isolatie dikte
{
Isulation = Regel.SubString(4,(Regel.Length()-3));
Regel = "";
}
if(Code == "NW") //Gewicht
{
Weight = Regel.SubString(4, (Regel.Length()-3));
Regel = "";
}
if(Code == "CO") //Contour
{
String geheleRegel = Regel.SubString(4, (Regel.Length() -3));
int EleNr = 0;
int Status = 0;
//geheleRegel doormidden hakken voor x en y waardes.
String X = geheleRegel.SubString(0, (geheleRegel.Length() /2));
String Y = geheleRegel.SubString(geheleRegel.Length()/2 +1, geheleRegel.Length());
LinePiece lpObject(Code, EleNr, Status, StrToInt(X), StrToInt(Y));
LPArray[LPIndex] = lpObject;
LPIndex++;
Regel = "";
}
if(Code == "*" && firstElement == false) //Nieuw element
{
if(OrderNumber == "")
{
OrderNumber =="-";
}
if(OrderBrand == "")
{
OrderBrand = "-";
}
if(ElementNumber == "")
{
ElementNumber = "-";
}
if(ElementMarking == "")
{
ElementMarking = "-";
}
if (ReinforcementPattern == "")
{
ReinforcementPattern = "-";
}
if (CalculationNumber == "")
{
CalculationNumber = "-";
}
if(ElementLength == "")
{
ElementLength = 0;
}
if(ElementWidth == "")
{
ElementWidth = 0;
}
if(ElementBeginX == "")
{
ElementBeginX = 0;
}
if(ConcreteThickness == "")
{
ConcreteThickness = 0;
}
if(Isulation == "")
{
Isulation = 0;
}
if(Weight == "")
{
Weight = 0;
}
element ElementObject(OrderNumber, OrderBrand, ElementNumber, ElementMarking,
ReinforcementPattern, CalculationNumber, StrToInt(ElementLength), StrToInt(ElementWidth),
StrToInt(ElementBeginX), StrToInt(ConcreteThickness),StrToInt(Isulation),
Weight, LPArray);
ElementArray[Index] = ElementObject;
LPIndex = 0; /resetting LPIndex
Index++;
}
else
{
Regel="";
}
if (Code == "*")
{
firstElement = false;
}
}
}
}
return ElementArray;
}
因此,出于某种原因,它并没有使X&#39; X&#39;不同的LPArrays,但覆盖所有LParray与最后一个。我该如何解决这个问题?
答案 0 :(得分:2)
您的初始化代码应如下所示:
element *ElementArray = new element[100];
for ( int n = 0; n < 100; ++n )
ElementArray[n].arrayLP = new LinePiece[100];
所以你描述的行为可能是由于未定义的行为
你似乎没有在任何地方删除你分配的数组 - 这将导致内存泄漏。此外,如果你想要数组而不是向量,那么你必须遵守三条规则(添加复制构造函数/运算符和析构函数)。