Undefined reference to 'Class::Class'

63,886

Solution 1

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

Solution 2

I had this kind of error and the cause was that the CombatAdmin.cpp file wasn't selected as a Build target file: Prject->Properties->Build targets

Solution 3

Are you sure you've to include your header as:

#include <CombatAdmin.h>

?

I think you need to include your header file as:

#include "CombatAdmin.h"

And same for other headers written by you, like these:

#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 "filename"?

Solution 4

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

class CombatAdmin;

Share:
63,886
James
Author by

James

Updated on March 23, 2020

Comments

  • James
    James about 4 years

    After fixing the previous problem (see my one other question that I have asked). I had declared more classes.

    One of these is called CombatAdmin which does various things: (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
    

    It is then instanced in the main.cpp file like this: (Snippet 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'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
    

    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 (ver 10.05), with the GNU GCC compiler. The project is of type "Console application". I am using windows XP 32 bit SP3.

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

    As can be seen from the code, the narrator and PIbot are instanced just fine. (then used, not shown)

    My question is, therefore, what do I need to do to stop these errors occurring? As when I encountered similar "Undefined reference to x" errors before using libraries. I had just forgotten to link to them in Code::Blocks and as soon as I did, they would work.

    As this class is of my own making I am not quite sure about this.

    Do say if you need more information regarding the code etc.

    • trojanfoe
      trojanfoe about 13 years
      Can you show the implementation file (CombatAdmin.cpp) ?
    • James
      James about 13 years
      @trojanfoe It is very big, and I have now solved the problem thanks to help from here.
    • James
      James about 13 years
      Thank you for the help everyone, once again I am in your debt.
  • James
    James about 13 years
    You are right! I can't believe I made such a stupid mistake :S
  • James
    James about 13 years
    Nope, #include <CombatAdmin.h> works fine, it was just another mistake on my part. (see my accepted answer)
  • James
    James about 13 years
    But yes, I think I should look over the difference again, thanks.
  • itsols
    itsols almost 11 years
    @zvrba I'm glad you found the problem. I'm a little lost here. What do you mean by providing an implementation*? Thanks!