Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]

Author Topic: Need help with c++ , urgent!  (Read 3305 times)

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Need help with c++ , urgent!
« Reply #15 on: April 05, 2009, 02:19:05 pm »

You could use an array, a vector or even some variation of linked list.

Each has it's advantages.


Also, I think that there *may* be a function tot increase the size of an array automatically moving it if it needs it.
Logged
Eh?
Eh!

Dasleah

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #16 on: April 05, 2009, 04:20:34 pm »

I need it to work for my rougelike game.

Wow, a make-up based game. That sounds interesting ;)

Anyway, I wonder where would of been a perfect place to also post this question? If only someone had made a megathread, oh dear.
Logged
Pokethulhu Orange: UPDATE 25
The Roguelike Development Megathread.

As well, all the posts i've seen you make are flame posts, barely if at all constructive.

Mcshay

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #17 on: April 05, 2009, 04:45:41 pm »

Err I don't think so... ::)

Also, in my character class:

Code: [Select]
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:
       
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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)
« Last Edit: April 05, 2009, 04:48:23 pm by Mcshay »
Logged

alfie275

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #18 on: April 06, 2009, 04:24:08 pm »

Ok got that working, but now I have another problem. Basically I need to either convert a string or char to a BodyPart or use a string or char as the name for a new BodyPart variable, how do I do this without the getting shadowing errors?
Logged
I do LP of videogames!
See here:
http://www.youtube.com/user/MrAlfie275

Mcshay

  • Bay Watcher
    • View Profile
Re: Need help with c++ , urgent!
« Reply #19 on: April 06, 2009, 07:20:46 pm »

I'm not sure what the problem is. Could you provide an example of what you're trying to do?
Logged
Pages: 1 [2]