Reputation: 736
I'am novice in C++ and I don't understand how headers, class, object and inheritance work.
So I've problem in my code. I can't call my method attack from class Mage inherit from class Character.
When i try to test happen this error:
CMakeFiles\GUi.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Guilherme/Dropbox/Develop/c++/t/main.cpp:8: undefined reference to `Mage::attack()'
I really don't know where the problem. If someone can help me i'll be glad.
This main.cpp
#include <iostream>
#include <conio.h>
#include <string.h>
#include "chars/Mage.h"
int main() {
Mage myChar;
myChar.attack();
return 0;
}
Mage.h
class Mage{
public:
void attack();
};
Mage.cpp
#include "Character.h"
class Mage : public Character{
public:
double maxHP = 20;
void attack(){
}
};
Character.h
#define MINIMUM_POINTS 1
#define STATUS_CHARACTER_DEAD false
#define STATUS_CHARACTER_LIVED true
#include <string.h>
class Character{
public:
Character();
int level; //current level
double maxHP; //Max Life
double maxMP; //Max points to use mana
double attackPhysical; //Power physical attack
double attackMagic; // Points to magic attack
double defensePoints; // Point to physical attack defense
// string name; //Char name
bool live; // if person is live or dead
void setName(); //Setter Char name
void getName(); //Getter char name
void attack();
};
**Character.cpp*
class Character{
public:
void attack(){
};
};
Upvotes: 0
Views: 88
Reputation: 1980
In Mage.cpp:
void Mage::attack(){
}
also in Character.cpp:
void Character::attack(){
};
Upvotes: 1
Reputation: 286
You must have just one definition of the 'class Mage'. You have two and they contradict each other. One is in Mage.h and the other in Mage.cpp. You should have only the implementation of Mage::attack in Mage.cpp in this way:
void Mage::attack(){
}
Also you do need to understand how header files work in C/C++ before anything else.
Upvotes: 1