2011-03-22 80 views
4

解決了之前的問題後(​​請參閱我提出的另一個問題)。我已經宣佈了更多的課程。對'Class :: Class'的未定義引用

其中之一是稱爲CombatAdmin其中做各種事情:(頭文件)

#ifndef COMBATADMIN_H 
#define COMBATADMIN_H 

#include <string> // Need this line or it complains 
#include <Player.h> 
#include <Sound.h> 
#include <Enemy.h> 
#include <Narrator.h> 
using namespace std; 

class Enemy; 
class Player; 

class CombatAdmin // Code yet to be commented here, will come soon. 
{ 
    public: 
     CombatAdmin(); 
     void healthSet(double newHealth, string playerName); 
     void comAdSay(string sayWhat); 
     void playerFindsChest(Player *player,Weapon *weapon,Armour *armour); 
     void youStoleOurStuffEncounter(Player *player); 
     void comAdWarning(string enemyName); 
     void comAdAtkNote(string attack, double damage,string target,string aggresor); 
     void entDefeated(string entName); 
     void comAdStateEntHp(string ent, double hp); 
     void comAdStateScanResults(string enemyName, double enemyHealth); 
     string doubleToString(double number); 
     string intToString(int number); 
     bool isRandEncounter(); 
     void randomEncounter(Player *player,Sound *sound,Narrator *narrator); 
     bool combatRound(Player *player, Enemy *enemy, Sound *sound, bool ran); 
     void playerFindsItem(string playerName,string itemName,double itemWeight,double playerWeight); 
     void playerFindsGold(string playerName,double coinCnt,double playerCoinCnt); 

}; 

#endif // COMBATADMIN_H 

它然後在main.cpp中文件這樣實例化:(所述的main.cpp文件的摘錄)

#include <iostream> // Required for input and output 
#include <Item.h> // Item header file. 
#include <Weapon.h> // Header files that I have made for my classes are needed for this program 
#include <sstream> // Needed for proper type conversion functions 
#include <windows.h> // for PlaySound() and other functions like sleep. 
#include <time.h> // Needed to seed the rand() function. 
#include <mmsystem.h> // Not sure about this one, possibly defunct in this program. 
#include <stdio.h> // Needed for a similar kind of output as iostream for various functions error msgs. 
#include <irrKlang.h> // The header file of the sound lib I am using in this program. 
#include <Narrator.h> // The narrators's header file. 
#include <Pibot.h> // Other header files of classes. 
#include <Armour.h> 
#include <Player.h> 
#include <Weapon.h> 
#include <CombatAdmin.h> 

using namespace irrklang; 

using namespace std; 

// Forward referenced functions 


void seedRandom(); // Seeds the random number so it will be random as apposed to pseudo random. 
string getPlayerName(string temp); // Gets the player's new name. 


int main(int argc, char* argv[]) 
{ 
    // Variables and object pointers declared here. 
    CombatAdmin *comAd = new CombatAdmin(); // Handles combat. 
    Narrator *narrator = new Narrator(); // The Narrator that says stuff 
    Pibot *piebot = new Pibot(); // PIbot, the player's trusty companion 

    string temp; // Temp string for input and output 

然而,當我嘗試編譯項目時,我得到以下錯誤:

C:\Documents and Settings\James Moran.HOME-B288D626D8\My Documents\C++ projects\Test Project\main.cpp|59|undefined reference to `CombatAdmin::CombatAdmin()'| 

我正在使用Code :: Blocks IDE(版本10.05)和GNU GCC編譯器。該項目是「控制檯應用程序」類型。我正在使用Windows XP 32位SP3。

我試過改變搜索目錄來包含目標文件所在的位置,但沒有成功。

從代碼可以看出,敘述者和「PIbot」的實例很好。 (然後使用,未顯示)

因此,我的問題是,我需要做些什麼來阻止這些錯誤的發生?就像我在使用庫之前遇到類似的「Undefined reference to x」錯誤一樣。我剛剛忘記了在Code :: Blocks中鏈接到它們,只要我這樣做,它們就會工作。

由於這個班是我自己製作的,我對此不太確定。

難道說,如果你需要更多關於你聲明的默認構造函數(CombatAdmin()),從而阻止了編譯器自動生成它的代碼等

+0

你可以顯示執行文件(CombatAdmin.cpp)嗎? – trojanfoe 2011-03-22 15:10:31

+0

@trojanfoe這是非常大的,我現在已經解決了這個問題,感謝來自這裏的幫助。 – James 2011-03-22 15:21:46

+0

謝謝大家的幫助,我再次在你的債務。 – James 2011-03-22 15:23:22

回答

16

更多信息。因此,您需要1)從類中移除默認構造函數的聲明,或者2)提供一個實現。

+0

你是對的!我不敢相信我犯了這樣一個愚蠢的錯誤:S – James 2011-03-22 15:19:00

+1

@zvrba我很高興你發現了這個問題。我有點迷失在這裏。你提供一個實現**意味着什麼?謝謝! – itsols 2013-05-20 07:35:22

1

你確定你已經包含您的標題爲:

#include <CombatAdmin.h> 

我認爲你需要包括你的頭文件爲:

#include "CombatAdmin.h" 

而同樣由你書面其它頭,像這樣的:

#include "Armour.h" 
#include "Player.h" 
#include "Weapon.h" 
//and similarly other header files written by you! 

請參見本主題:

What is the difference between #include <filename> and #include "filename"?

+0

不,#include 工作正常,這只是我的另一個錯誤。 (請參閱我接受的答案) – James 2011-03-22 15:21:17

+0

但是,我認爲我應該再次查看差異,謝謝。 – James 2011-03-22 15:26:28

2

我有這種錯誤,原因是沒有選擇CombatAdmin.cpp文件作爲生成目標文件:項目 - >屬性 - >生成目標

-1

我的解決方案只是在標題之前添加一行班級辯護:

class CombatAdmin;

相關問題