首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对“Class::Class”的引用未定义

对“Class::Class”的引用未定义
EN

Stack Overflow用户
提问于 2011-03-22 23:05:26
回答 4查看 63.1K关注 0票数 7

在修复了前面的问题之后(参见我提出的另一个问题)。我声明了更多的类。

其中一个叫做CombatAdmin,它做各种事情:(头文件)

代码语言:javascript
复制
#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文件的片段)

代码语言:javascript
复制
#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

然而,当我尝试编译这个项目时,我得到了以下错误:

代码语言:javascript
复制
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::块中链接到它们,一旦我这样做了,它们就会工作。

因为这个类是我自己做的,所以我不太确定这一点。

如果你需要更多关于代码的信息,一定要说出来。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-22 23:09:24

您已经声明了默认构造函数(CombatAdmin()),因此阻止了编译器自动生成它。因此,您需要1)从类中移除默认构造函数的声明,或者2)提供一个实现。

票数 23
EN

Stack Overflow用户

发布于 2013-02-15 23:08:20

我遇到了这种错误,原因是没有选择CombatAdmin.cpp文件作为构建目标文件:

票数 3
EN

Stack Overflow用户

发布于 2011-03-22 23:11:24

是否确实要将标头包括为:

代码语言:javascript
复制
#include <CombatAdmin.h>

我认为你需要包括你的头文件如下:

代码语言:javascript
复制
#include "CombatAdmin.h"

对于由you编写的其他headers 也是如此,如下所示:

代码语言:javascript
复制
#include "Armour.h"
#include "Player.h"
#include "Weapon.h"
//and similarly other header files written by you!

请参阅此主题:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5393293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档