Undefined reference to "Class :: Class"

After fixing the previous problem (see my one more question I asked). I announced more classes.

One of them is called CombatAdmin, which performs various actions: (header file)

#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 

Then it is created in the main.cpp file as follows: (fragment of the main.cpp file)

 #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 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 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 trusty companion string temp; // Temp string for input and output 

However, when I try to compile the project, I get the following error:

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

I am using the Code :: Blocks IDE (version 10.05) with the GNU GCC compiler. The project is of the type "Console Application". I am using Windows XP 32 bit SP3.

I tried changing the search directories to include where the object files are, but there is no success.

As you can see from the code, the storyteller and PIbot instantiate just fine. (then used, not shown)

My question is, therefore, what do I need to do to stop these errors? Like when I came across similar "Undefined reference to x" errors before using libraries. I just forgot to link them in Code :: Blocks, and as soon as I did, they will work.

Since this class is my own, I'm not quite sure about it.

Tell me if you need more information about the code, etc.

+4
source share
4 answers

You declared the default constructor (CombatAdmin ()) and thus prevented the compiler from automatically generating it. Thus, you either need to: 1) remove the default constructor declaration from the class, or 2) provide an implementation.

+16
source

I had such an error, and the reason is that the CombatAdmin.cpp file was not selected as the Build target file: Prject-> Properties-> Build target

+2
source

Are you sure you should include your title as:

 #include <CombatAdmin.h> 

?

I think you need to include your header file as:

 #include "CombatAdmin.h" 

And for other headers written by you , for example:

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

See this topic:

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

+1
source

My solution was only to add a line to the header before the defenition class:

class CombatAdmin;

-1
source

Source: https://habr.com/ru/post/1344782/


All Articles