对 'Class::Class' 的未定义引用

7

在解决了之前的问题后(请参见我提出的另一个问题),我声明了更多的类。

其中之一被称为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已经被实例化得很好。(然后使用,未显示)
因此,我的问题是,我需要做什么来阻止这些错误的发生?因为当我在使用库之前遇到类似的“未定义引用x”错误时。我只是忘记在Code::Blocks中链接它们,一旦我链接了它们,它们就会起作用。
由于这个类是我自己创建的,所以我不太确定这一点。
如果您需要有关代码等更多信息,请告诉我。

你能展示一下实现文件(CombatAdmin.cpp)吗? - trojanfoe
@trojanfoe 这个问题很大,但是我现在已经通过这里的帮助解决了。 - James
谢谢大家的帮助,我再次感激不尽。 - James
4个回答

23

你已经声明了默认构造函数 (CombatAdmin()),这样就防止编译器自动生成它。因此,你需要做两件事中的一件:1)从类中移除默认构造函数的声明,或者2)提供一个实现。


你说得对!我简直不敢相信我犯了这么愚蠢的错误:S - James
1
@zvrba,我很高兴你找到了问题所在。我有点迷失了。你所说的“提供实现”是什么意思?谢谢! - itsols

3

我曾遇到过这种错误,原因是CombatAdmin.cpp文件未被选择为构建目标文件:项目->属性->构建目标


2

您确定要将头部包含为以下内容:

#include <CombatAdmin.h>

我认为你需要将头文件包含进来,写法如下:

#include "CombatAdmin.h"

还有其他由您编写的标题,比如这些:

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

请查看此主题:

#include <filename>和#include "filename"有什么区别?


不,#include <CombatAdmin.h> 没有问题,这只是我犯的另一个错误。(请参见我的已接受答案) - James
但是,我认为我应该再次查看差异,谢谢。 - James

1
我的解决方案只是在类定义之前在头文件中添加了一行代码: CombatAdmin;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接