Err I don't think so...
Also, in my character class:
void Addpart(BodyPart* part,char initName, int initHealth, BodyPart* initParent, bool initHasParent) {
int i;
OldParts = new BodyPart[Numparts];
for (i = 0;i < Numparts; i++) {
OldParts[i] = Parts[i];
}
delete [] Parts;
Numparts = Numparts + 1;
Parts = new BodyPart[Numparts];
Parts[Numparts] = part;
part.Init(initName, initHealth, initParent, initHasParent);
for (i = 0;i < Numpats - 1; i++) {
Parts[i] = OldParts[i]
}
delete [] OldParts;
}
Throws the errors:
83 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp no match for 'operator=' in '*(((Character*)this)->Character::Parts + (+(((unsigned int)((Character*)this)->Character::Numparts) * 20u))) = part'
note E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp:16 candidates are: BodyPart& BodyPart::operator=(const BodyPart&)
84 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp `Init' is not a type
84 E:\Alfie\Ascii Man\AsciiMan alpha\main.cpp request for member of non-aggregate type before '(' token
I can't do much to help you debug that without some additional information:
- What is the line number of the first line in the quoted code? Errors aren't useful unless I see the context.
- Where is OldParts defined? That snipit of code assumes it is already declared.
I can help a bit though with what I have now:
- You've made a few indexing errors like this one:
Parts[Numparts] = part;
Which will put the address stored in 'part' into invalid memory (you already made 'Numparts' the size of the new array, so that index will be for the 'Numparts + 1' item, which does not exist).
Also, could you use the code block tags in the future? Some important parts of your code were turned into forum tags without them.
If you want a general way to append a DIY dynamic array, this template function I just threw together should do it:
template<class Obj> bool AppendArray(Obj*& pArray, int& iLength, const Obj& oAdd)
{
if(iLength < 0) //Error on impossible lengths
return false;
if(iLength) // if > 0
{
//Remember the old array and allocate a new one
Obj* pTemp = pArray;
pArray = new Obj[iLength + 1];
//Copy over the old data
for(int i = 0; i < iLength; i++)
pArray[i] = pTemp[i];
//Clean up
if(iLength > 1) //Make sure we use the right delete
delete [] pTemp;
else
delete pTemp;
}
else
pArray = new Obj; //just needs 1, no old contents
//add the new item in the last place, then increment the length
pArray[iLength++] = oAdd;
return true;
}
Note that all three of the arguments are references, so you can use it like so:
int* p;
int len = 0;
AppendArray(p, len, 1);
AppendArray(p, len, 2);
for(int i = 0; i < len; i++)
cout << p[i];
Which will fill an empty array with the integers 1 and 2 and then print them out.
(Yes I am bored)