I have been learning c++ and Allegro (graphics libary) for about a
year, and have been trying to get this code to work (in particular,
passing the array of classes), and when compiling i get the following errors

C:\Documents and Settings\User\Desktop\npc.h In member function `void cNPC::NPCmove(cNPC*)':
96 C:\Documents and Settings\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, int, short unsigned int&)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
107 C:\Documents and Settings\User\Desktop\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, short unsigned int&, int)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
118 C:\Documents and Settings\User\Desktop\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, int, short unsigned int&)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
129 C:\Documents and Settings\User\Desktop\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, short unsigned int&, int)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)

I have tried passing the entire class (without the '&' and with '[10]')

NPCmovecheck(npcs[10],itsx-4,itsy)

and this works, but i would perfer to use a more efficient method.

If anyone can help it would be much appreciated.

class cNPC
{
public:
    cNPC();
    ~cNPC();
    unsigned short int getx(); // returns x
    unsigned short int gety(); // returns y
    void setx(unsigned short int newx);
    void sety(unsigned short int newy);
    char getimageID(); // returnes image id
    void NPCmove(cNPC * npcs); // controls movement
    bool NPCmovecheck(cNPC * npcs, unsigned short int xto, unsigned short int yto); //check if it can move //add xto yto
private:
    unsigned short int itsx; // x co-ordinate
    unsigned short int itsy; // y co-ordinate
    char itsimageID; // image id for npc map
    char itsdirection; // current direction
    char itsframe; // current frame count [ 12 = stop moving]
    bool itsmoving; // am i currently moving
    bool itsspecial; // is the npc different - ID for special stuff
    bool itsdoesmove; //does the npc move
};

cNPC::cNPC()
{
    itsx = 128; // x co-ordinate
    itsy = 128; // y co-ordinate
    itsimageID = 0; // image id for npc map
    itsdirection = 0; // current direction
    itsframe = 0; // current frame count [ 12 = stop moving]
    itsmoving = false; // am i currently moving
    itsspecial = false; // is the npc different - ID for special stuff
    itsdoesmove = true; //does the npc move
}

cNPC::~cNPC()
{
    //nothing - deconstructor
}

void cNPC::setx(unsigned short int newx)
{
    itsx = newx;
}

void cNPC::sety(unsigned short int newy)
{
    itsy = newy;
}

unsigned short int cNPC::getx()
{
    return itsx;
}

unsigned short int cNPC::gety()
{
    return itsy;
}

//returnes true if no collision [ie. you can move]
bool cNPC::NPCmovecheck(cNPC * npcs, unsigned short int xto, unsigned short int yto)// add include xto yto
{
    bool answer = true;
    unsigned short int theirx;
    unsigned short int theiry;
    //for every obj check
    for (char forx=0; forx < 10; forx ++)
    {
        theirx = npcs[forx].getx();
        theiry = npcs[forx].gety();
        if (xto < theirx + 32 && xto > theirx-32) //if x collision
        {
            if (yto < theiry + 32 && yto > theiry - 32) // if y collision
            {
                answer = false;
            }
        }
    }
    //map check NOT YET IMPLIMENTED
    return answer;
}

//THE ERROR OCCURES IN THIS FUNCTION
void cNPC::NPCmove(cNPC * npcs)
{
    if (itsdoesmove==true) // am i allowed to move
    {
        if (itsmoving==true) // am i still moving
        {
            switch (itsdirection)
            {
            case 0:
                if (NPCmovecheck(&npcs,itsx+4,itsy)==true) // here
                {
                    itsx += 4;
                }
                else //if cannot move
                {
                    itsframe = 0;
                    itsmoving = false;
                }
                break;
            case 1:
                if (NPCmovecheck(&npcs,itsx,itsy-4)==true) // here
                {
                    itsy -= 4;
                }
                else //if cannot move
                {
                    itsframe = 0;
                    itsmoving = false;
                }
                break;
            case 2:
                if (NPCmovecheck(&npcs,itsx-4,itsy)==true) // here
                {
                    itsx -= 4;
                }
                else //if cannot move
                {
                    itsframe = 0;
                    itsmoving = false;
                }
                break;
            case 3:
                if (NPCmovecheck(&npcs,itsx,itsy+4)==true) // and here
                {
                    itsy += 4;
                }
                else //if cannot move
                {
                    itsframe = 0;
                    itsmoving = false;
                }
                break;
            }
            itsframe += 1;
            if (itsframe == 12)
            {
                itsframe = 0;
                itsmoving = false;
            }
        }
        else //if not moving      ---  else if itsmoving == false
        {
            if (rand() % 50 == 0)
            {
                itsmoving = true;
                itsframe = 0;
                itsdirection = rand() % 4;
            }
        }
    }
}

If anyone can help it would be much appreciated.

Recommended Answers

All 2 Replies

From what I can see the function in question takes a pointer to a cNPC object, not a pointer to an array of cNPC objects.

Would it make more sense to store your cNPC objects in something like a std::vector instead of an array?
e.g. std::vector<cNPC>

That way you can alter your functions signature to pass the vector into your functions as a reference or a const reference instead of using a pointer to an array. It's just as efficient as using a pointer. It's probably also safer and less prone to bugs/errors than using a pointer to an array too!

Saying that, if you don't want to use the std::library, there's nothing stopping you from passing your array into your function as a reference or a const reference either.

Offhand, those are two options that I can see! But I'm sure there are many other ways around this problem!

Cheers for now,
Jas.

{edit: BTW, if I am way off the mark here I apologise. It is past 3am here, so tiredness is kicking in with a vengeance, I may have misread something..Think I'm gonna have to crash soon though. I've got work in a few hours...Doh! }

Thanks, I didn't think of passing a refrence.
I got a lot of errors, because I wrote

void cNPC::NPCmove(cNPC &npcs[10])

instead of

void cNPC::NPCmove(cNPC (&npcs)[10])

It said I was using an array of refrences, but at least it worked.
Off topic, (and you don't have to answer) why were the brackets nesissary? I notice they are also used in binary file i/o

fout.read( (char *)(some variable);

If anyone wants to satisfy my curiosity, feel free.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.