如何使子类使用父类函数?

时间:2019-04-04 23:37:21

标签: visual-c++

我正在尝试制作一种具有不同类型的代理的游戏。我正在用虚拟方法定义一些方法,这些方法在父类“ Agent”中定义,我希望子类“ Tanc”和“ Asasin”才能使用相同的方法。但是,它不起作用,并且程序会以未解决的错误外部符号结束。

例如,如果我要定义代理TancX。 如果我尝试调用x.Get_Attack(),它将无法正常工作。

我试图在网络上搜索有关此问题的一些解释,但似乎无济于事。

这是我的Agenti.h

#pragma once
#include <iostream>
#include <string>

class GameMode;

class Agent
{
private:

    int id;
    std::string nume;
    int viata;
    int atac;
    int aparare;
    int scut;
    int viteza;
    enum Iteme
    {

    };
    std::pair<int, int > pozitie;

public:
    Agent();
    virtual int Get_Health() { return viata; }
    virtual int Get_Attack() { return atac; }
    virtual int Get_Defence() { return aparare; }
    virtual int Get_Shield() { return scut; }
    virtual int Get_Speed() { return viteza; }
    virtual std::string Get_Name(){ return nume; }
    virtual int Get_ID() { return id; }
    virtual std::pair<int,int> Get_Position() { return pozitie; }
    virtual void Set_Position(GameMode& obj);
};

class Tanc : public Agent
{
private:
    int id = 1;
    std::string nume;
    int viata = 150;
    int atac = 10;
    int aparare;
    int scut;
    int viteza;
    std::pair<int, int > pozitie;
    enum Iteme
    {

    };
public:
    Tanc();
    int Get_Health() override;
    int Get_Attack() override;
    int Get_Defence() override;
    int Get_Shield() override;
    int Get_Speed() override;
    std::string Get_Name() override;
    int Get_ID() override;
    std::pair<int, int> Get_Position() override;
    void Set_Position(GameMode& obj) override;
};

class Asasin : public Agent
{
private:
    int id;
    std::string nume;
    int viata;
    int atac;
    int aparare;
    int scut;
    int viteza;
    std::pair<int, int > pozitie;
    enum Iteme
    {

    };
public:
    Asasin();
    int Get_Health() override;
    int Get_Attack() override;
    int Get_Defence() override;
    int Get_Shield() override;
    int Get_Speed() override;
    std::string Get_Name() override;
    int Get_ID() override;
    std::pair<int, int> Get_Position() override;
    void Set_Position(GameMode& obj) override;
};

这是我的Agenti.cpp

#include "pch.h"
#include "Agenti.h"
#include "GameMode.h"
#include <fstream>
#include <ctime>

std::ifstream f("Nume_Agenti.txt");

Agent::Agent()
{
    this->aparare = 0;
    this->atac = 0;
    this->viata = 0;
    this->id = 0;
    this->nume = "";
    this->scut = 0;
    this->viteza = 0;
}

void Agent::Set_Position(GameMode& obj)
{
    srand((int)time(0));
    int **harta = obj.Get_The_Map();
    do
    {
        this->pozitie.first = rand() % obj.Aflam_lungimea();
        this->pozitie.second = rand() % obj.Aflam_latimea();

    } while (harta[pozitie.first][pozitie.second] != 0);
}

Asasin::Asasin()
{
    this->id = 2;
    f >> this->nume;
    this->viata = 75;
    this->atac = 45;
    this->aparare = 10;
    this->scut = 0;
    this->viteza = 3;
}

Tanc::Tanc()
{
    this->id = 1;
    f >> this->nume;
    this->viata = 150;
    this->atac = 10;
    this->aparare = 15;
    this->scut = 50;
    this->viteza = 1;
}

我希望它能正常工作,但正如我所说的那样,它以无法解决的外部符号(LNK 2001)结束

0 个答案:

没有答案