解決了之前的問題後(請參閱我提出的另一個問題)。我已經宣佈了更多的課程。對'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()),從而阻止了編譯器自動生成它的代碼等
你可以顯示執行文件(CombatAdmin.cpp)嗎? – trojanfoe 2011-03-22 15:10:31
@trojanfoe這是非常大的,我現在已經解決了這個問題,感謝來自這裏的幫助。 – James 2011-03-22 15:21:46
謝謝大家的幫助,我再次在你的債務。 – James 2011-03-22 15:23:22