1

I am creating a program to practice my knowledge on classes and files but I can get it to work. Previously I was getting an error saying that robots::robots() was defined multiple times but now I have this error saying undefined reference to 'robots::robots()'. Here is the code for enemy_definitions.h:

#include <iostream>
using namespace std;

class enemies
{
    public:
    string name;
    int hp;
    int damage;
    virtual void print_information();
};

class robots: public enemies
{
    public:
        robots();
        void print_information();
    private:
        int power_requirement;
};

class zombies: public enemies
{
    public:
        void print_information();
    private:
        int height;
};

class aliens: public enemies
{
    public:
        void print_information();
    private:
        string colour;
};

Here is the code for enemy_definitions.cpp:

#include <iostream>
#include "enemy_definitions.h"

void enemies :: print_information()
{
}

robots :: robots()
    {
        cout <<"Name: ";
        cin >> name;
        cout <<"\nhp: ";
        cin >> hp;
        cout <<"\ndamage: ";
        cin >> damage;
        cout <<"\n power_requirement: ";
        cin >> power_requirement;
    }

void robots :: print_information()
{
    cout << this->name << " has ";
    cout << this->hp << "hit-points, ";
    cout << this->damage << " damage and ";
    cout << this->power_requirement << "power requirement";
}

void zombies :: print_information()
{
    cout << this->name << " has ";
    cout << this->hp << "hit-points, ";
    cout << this->damage << " damage and ";
    cout << this->height << "height";
}

void aliens :: print_information()
{
    cout << this->name << " has ";
    cout << this->hp << "hit-points, ";
    cout << this->damage << " damage and ";
    cout << this->colour << "colour";
}

and here is the code for main.cpp:

#include <iostream>
#include "enemy_definitions.h"

using namespace std;

int main()
{
robots Bertha;
Bertha.print_information();
}

Can someone spot why I keep getting this error.

  • 2
    Don't ever put `using namespace std;` in the global namespace in a header. For example, the standard library defines the names `count` and `distance`. They can easily collide with names in the code where the header is included. – Cheers and hth. - Alf Jul 05 '18 at 09:54
  • How are you compiling/linking? – Algirdas Preidžius Jul 05 '18 at 09:56
  • 2
    "undefined reference" means that you're not linking with object code or a library that defines the thing. For more detailed help you need to provide the exact build commands you use. – Cheers and hth. - Alf Jul 05 '18 at 09:56
  • Seems like a configuration problem. How do you build your executable? – StoryTeller - Unslander Monica Jul 05 '18 at 09:56
  • Just a guess: [run a program with more than one source files in GNU c++ compiler](https://stackoverflow.com/questions/11155327/run-a-program-with-more-than-one-source-files-in-gnu-c-compiler) – Bo Persson Jul 05 '18 at 09:59
  • @Cheers and hth - Alf,I am quite new to C++ so I dont know any different than to use using namespace std; –  Jul 05 '18 at 10:24
  • @Algirdas Preidzius what do you mean –  Jul 05 '18 at 10:25
  • @Cheers and hth - Alf, what are build commands? –  Jul 05 '18 at 10:26
  • @ StoryTeller im not sure, I use codeblocks and I created a project then added a header file and a .cpp file then wrote the code –  Jul 05 '18 at 10:28
  • @forwhenotheracount'sbanned 1) What is the command line, with which you are compiling/linking? Or, what is the project configuration, in case you are using IDE? 2) "_I am quite new to C++ so I dont know any different than to use using namespace std;_" Consider reading: [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), and consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Jul 05 '18 at 10:33
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tadeusz Kopec for Ukraine Jul 05 '18 at 11:45
  • Tadeusz Kopec I dont know what that is so it isnt a duplicate –  Jul 05 '18 at 13:26

1 Answers1

1

You are only compiling your main.cpp file that is why the compiler is not able to link your definition of functions. Use

g++ main.cpp enemy_definitions.cpp

This should link your code properly.

random40154443
  • 1,100
  • 1
  • 10
  • 25
  • But from the book I am reading I havent needed to use g++ and the person who has made the book managed to link files without having to use g++ so how could I do it without using g++ –  Jul 05 '18 at 13:30
  • How is the book telling you to compile then ? – random40154443 Jul 05 '18 at 13:31
  • Well he said to create an empty source file to your current project then select debug and release and it should work –  Jul 05 '18 at 18:42
  • Your IDE must be showing the build command it is using when you build. You can check if it compiling both files. – random40154443 Jul 05 '18 at 18:50