2014-10-05 56 views
0

我來找你有一個涉及幾個不同文件的問題。我不知道爲什麼我得到標題中指定的錯誤。讓我把下面的文件,並從那裏去。C++〜調用客戶端函數給出錯誤:「標識符____未定義」

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 

最後,Gameboard.cpp

#include "Gameboard.h" 

Gameboard::Gameboard() 
{ 

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

} 

我不希望被spoonfed和某人解決我的問題對我來說,我想弄清楚,我做錯了什麼,這樣剩餘我的項目並不像過去那樣顛簸。

回想我的錯誤,「識別」PopulateZombies「是不確定的」,我無法想象它爲什麼。這可能與我做事的範圍有關嗎?如果我遺漏了任何代碼(我沒有把所有的東西都放在那裏,但我認爲我有一切相關的東西),只要讓我知道,我可以在需要的時候來回交談。

謝謝大家提前試圖幫助:)

- 安東尼

回答

1

一般情況下,你使用一個變量,而不是直接調用它調用的函數,如果在一個類中定義:

ZombieListClass zombieList=new ZombieListClass(); // add a variable here 

ZombieRec zombieList[MAX_ZOMBIES]; 

zombieList.PopulateZombies(zombieList[MAX_ZOMBIES]); // See the difference? 
+0

你知道,在我發佈這個問題之後,我看了看Gameboard myGB;在客戶端文件的正上方。有時我會覺得這很愚蠢,以至於忽視這個。謝謝! – SiggyxLeGiiT 2014-10-05 18:24:04

0

我不確定您發佈的錯誤是否是唯一的錯誤。以下是我在你的main.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();//The constructor"Gameboard()" is automatically called when you defined 
         //myGB in the previous line, 

    ZombieListClass();//see Hai Bi's great answer on this one 

    ZombieRec zombieList[MAX_ZOMBIES];//ZombieRec is a member of ZombieListClass, use . to access it 

    PopulateZombies(zombieList[MAX_ZOMBIES]); //Also see Hai Bi's answer 

} 

我的建議是重溫constructor and class definition概念將雙手放在個問題是這樣了。

相關問題