You are here:

C++/Linker Error

Advertisement


Question
Please, tell me what I'm doing wrong with this code...

On the Main.cpp file:

#include "personagem3.h"
#include <cstdlib>
#include <iostream>

using namespace std;

int main(){
   cout << "Mini RPG" << endl;
   int turn=3,choise=0,hp1=0,mp1=0,hp2=0,mp2=0;
   cout << "Digite o HP(Health Points) do Jogador 1: " << endl;
   cin >> hp1;
   cout << "Digite o MP(Mana Points) do Jogador 1: " << endl;
   cin >> mp1;
   cout << "Digite o HP(Health Points) do Jogador 2: " << endl;
   cin >> hp2;
   cout << "Digite o MP(Mana Points) do Jogador 1: " << endl;
   cin >> mp2;
   
   Jogador1 j1;
   Jogador2 j2;
   j1.hpmp(hp1, mp1);   
   j2.hpmp(hp2, mp2);

   cout << "Jogador 1 tem " << j1._hp << "de HP" << endl;
   cout << "Jogador 2 tem " << j2._hp << "de HP" << endl;
   system("PAUSE");
  return 0;
}

On the Personagem4.cpp file:

#include "personagem3.h"

using namespace std;

void Personagem::hpmp(int hp, int mp)
{
         _hp= hp;
         _mp= mp;
}

void Personagem::set_hp(int hp)
{
  _hp = hp;
}

void Personagem::set_mp(int mp)
{
  _mp = mp;
}

and, on Personagem4.h file:

#ifndef __PERSONAGEM3_H_
#define __PERSONAGEM3_H_

using namespace std;

class Personagem
{
  public:
      Personagem();
      void hpmp(int hp, int mp);
      int _hp, _mp;
      void set_hp(int hp);
      void set_mp(int mp);
};

class Jogador1 : public Personagem
{
     public:
         Jogador1();
};

class Jogador2 : public Personagem
{
     public:
         Jogador2();
};

#endif


I know it's a bit big, but I would like the anwser until friday, if possible, ok?

Thanks in advance ;)


Answer
In the definition of classes Personagem, Jogador1 and Jogador2, you have declared constructors.
But you have not defined them - the linker is complaining that it can not find these constructors.

There are two possible fixes:

a. Define Personagem::Personagem(), Jogador1::Jogador1() and Jogador2::Jogador2() in the .cpp file

or

b. If there is nothing to be done in the constructors, just comment them out in the .h file


#ifndef __PERSONAGEM3_H_
#define __PERSONAGEM3_H_

using namespace std;

class Personagem
{
      public:
     //Personagem(); // ********
     void hpmp(int hp, int mp);
     int _hp, _mp;
     void set_hp(int hp);
     void set_mp(int mp);
};

class Jogador1 : public Personagem
{
    public:
        //Jogador1(); // ********
};

class Jogador2 : public Personagem
{
    public:
        //Jogador2(); // ********
};

#endif

Also, system("pause") represents a vulnerability (an unsafe programming practice) which can be exploited by an attacker. A malicious attacker could replace the "pause" program on the machine with a program that does some kind of damage, and use it to cause trouble. Spoofing a legitimate and harmless program is a common practice among malware.

In addition, it also has the drawback that the argument string is not portable across different implementations. Your code may work on windows, but may do something completely different (or not work at all) on unix.

A portable and less risky way is to write:

 std::cin >> std::ws ; // throw away white spaces remaining in the input buffer
 std::cin.get() ; // wait for the user to enter a new line  

instead.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.