Search code examples
c++fileclientundefinedidentifier

C++ ~ call to function in client gives error: "identifier ____ is undefined"


I'm coming to you with a problem that has several different files involved. I'm not sure why I'm getting the error specified in the title. Let me put the files below and go from there.

DummyClient.cpp

#include "Gameboard.h"          //for Gameboard
#include "Location.h"           //for function prototypes
#include "zList.h"              //for Zombies
#include <iostream>             //for input/output stream

using namespace std;

void main()
{
    srand(123456789);

    Gameboard myGB;

    myGB = Gameboard();

    ZombieListClass();

    ZombieRec zombieList[MAX_ZOMBIES];

    PopulateZombies(zombieList[MAX_ZOMBIES]); // this throws the error here of "Error: identifier "PopulateZombies" is undefined"

}

zList.h

#ifndef ZLIST_H
#define ZLIST_H

#include "Location.h"   // for list record
#include "ZombieRec.h"
#include "Gameboard.h"

class ZombieListClass
{

    public:
      ZombieListClass();            //default constructor 

      void PopulateZombies(ZombieRec zombieList[]);

      bool IsInBounds(int row, int col);


    private:
      ZombieRec list[MAX_ZOMBIES];      //stores the items in the list
      int length;                           //# of values currently in the list
      int currPos;                      //position of current element
      int strength;                 // health and attack units of a zombie

};

#endif

zList.cpp

#include "zList.h"

ZombieListClass::ZombieListClass()      //default constructor 
{

    length = 0;
    currPos = 0;
    strength = 5;
    LocationRec zombieLoc;

}

void ZombieListClass::PopulateZombies(ZombieRec zombieList[])
{
    int row, col;

    for (int i = 0; i < MAX_ZOMBIES; i++)
    {
        row = rand() % MAX_ROW + 1;
        col = rand() % MAX_COL + 1;

        while (!IsInBounds(row, col))
        {
            row = rand() % MAX_ROW + 1;
            col = rand() % MAX_COL + 1;
        }

        zombieList[i].currLoc.row = row;
        zombieList[i].currLoc.col = col;

    }


}

bool ZombieListClass::IsInBounds(int row, int col)
{

    if (row == 0 || row == MAX_ROW + 1 || col == 0 || col == MAX_COL + 1)
    {
        return false;
    }
    else
    {
        return true;
    }

}

Gameboard.h

#ifndef GAMEBOARD_H
#define GAMEBOARD_H


#include "Location.h" 
#include "ZombieRec.h"
#include "zList.h"


const int MAX_ROW = 3;      // total number of rows in the board
const int MAX_COL = 3;      // total number of cols in the board



class Gameboard
{

public:

    Gameboard();


private:
    int boardSizeArr[MAX_ROW + 2][MAX_COL + 2];


}; // end Gameboard

#endif

and finally, Gameboard.cpp

#include "Gameboard.h"

Gameboard::Gameboard()
{

    // Declares a board with a boundary along the outside
    boardSizeArr[MAX_ROW + 2][MAX_COL + 2]; 

}

I'm not looking to be spoonfed and for somebody to solve my problem for me, I'm trying to figure out what I'm doing wrong so that the remainder of my project isn't as bumpy as it has been this whole time.

Looking back on my error, "identifer "PopulateZombies" is undefined", I can't imagine why it is. Could this have something to do with the scope of how I'm doing things? If I've left any code out (I didn't put everything in there but I think I have everything relevant) just let me know, I'm able to converse back and forth as long as this takes.

Thank you to everybody in advance that tries to help :)

-Anthony


Solution

  • In general, you call the function using a variable, instead of calling it directly if defined in a class:

    ZombieListClass zombieList=new ZombieListClass();  // add a variable here
    
    ZombieRec zombieList[MAX_ZOMBIES];
    
    zombieList.PopulateZombies(zombieList[MAX_ZOMBIES]); // See the difference?