Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I keep getting the error "multiple definitions of robots::robots()" saying it is first defined on line 8. I cant spot the problem. Can someone see what is wrong?

Here is the main.cpp file:

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

using namespace std;

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


Here is the enemy_definitions.h file:
using namespace std;

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

class robots: public enemies
{
    robots();
    public:
        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;

};


and here is the enemy_definitions.cpp file:

C++
#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";


What I have tried:

things..................................................
Posted
Updated 4-Jul-18 10:05am
v3
Comments
Richard MacCutchan 3-Jul-18 3:28am    
Where is the implementation of the robots class?
BerthaDusStuf 4-Jul-18 16:07pm    
The defining of the class is in the enemy_definitions.h file under the definition for the enemies class and then the implementation of its constructor and print_information() function are in the enemy_definitions.cpp file
Richard MacCutchan 5-Jul-18 3:56am    
I just built this project and after correcting a couple of items it all compiles, links and runs. There is a missing semi-colon after the enemies class definition. And the robots constructor requires a public: modifier in front of it.
Richard MacCutchan 3-Jul-18 10:52am    
We cannot guess where the error messages occur. Please show all the code associated with the robots class, and indicate where the error message comes.
BerthaDusStuf 3-Jul-18 14:44pm    
that is all of the code and I said the error is on line 8 of enemy_definitions.cpp

The full error message contains more information: the name of the source file and the line number. Inspect that line and the preceeding lines, or show us the full message and indicate the line in the posted code.

However, your posted code snippets should result in an error in the aliens class:
class aliens: public enemies
{
    public;
        void print_information();
    private:
        string colour;
};
There is a semicolon instead of a colon after the public keyword.
 
Share this answer
 
Comments
CPallini 3-Jul-18 3:49am    
5. Good catch.
BerthaDusStuf 3-Jul-18 5:34am    
Thanks for the semi colon and the only other information it gives is the line number is 16 and the file is enemy_definitions.h
Jochen Arndt 3-Jul-18 16:15pm    
Due to a bug it is not possible to see the original (initially posted) version.

But I'm pretty sure that the robots::print_information() was public there while it is now declared as private.

See the similar code snippet from my answer (which I have copied and pasted) and compare it with your current question version:
class aliens: public enemies
{
private:
string colour;
void print_information();
};

The function is private there and also in the robots class. And private functions can be only called by the class itself, not by derived classes or by instances.

The function must be public.
BerthaDusStuf 4-Jul-18 7:12am    
But it is the class that is calling print_information. I created a class called Bertha and called print_information on the class itself so the function being private shouldnt be a problem
Jochen Arndt 4-Jul-18 11:08am    
No. You are calling it from an instance of the class. Private functions can be only called from functions which are class members itself. That's the reason of the existance of private members:
To deny access to them from class instances.

Also, please don't edit your question in a way you have done. My answer is now not related anymore to the current version (multiple definitions) while it was about trying to call a private function.

If a question has been solved, you can accept it. If you have a new question, raise a new one.
I would do it this way:
C++
#include <iostream>
using namespace std;

class enemy
{
public:
  virtual void print_information()
  {
    cout << "name " << name << ", hp " << hp << ", damage " << damage;
  }
private:
  string name{};
  int hp{};
  int damage{};
};

class robot: public enemy
{
public:
  virtual void print_information () override
  {
    enemy::print_information();
    cout << ", power_requirement " << power_requirement;
  }
private:
  int power_requirement{};
};



int main()
{
  robot bertha;
  bertha.print_information(); cout << endl;
}
 
Share this answer
 
Comments
BerthaDusStuf 3-Jul-18 5:37am    
I dont know what override does and also you used these brackets {} after declaring variables. I havent seen that before but I have seen these brackets () after variables to put something into their constructor.
CPallini 3-Jul-18 6:03am    
https://en.cppreference.com/w/cpp/language/override
https://en.cppreference.com/w/cpp/language/value_initialization
BerthaDusStuf 3-Jul-18 6:13am    
I think it is meant to be able to work without using override or the value initialization because neither of those things were taught in the book I am reading. Is there not just an error I can fix to make it work?
CPallini 3-Jul-18 6:32am    
While you CAN actually avoid using both override and uniform initialization, the fact 'they are not taught in the book' is not a good reason. On the contrary, consider your book is outdated.
BerthaDusStuf 3-Jul-18 6:37am    
ok thanks well I have found the fix to my problem and it was because I never actually defined the print_information function in the enemies superclass but now I have another problem so Im going to update my question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900