调用虚函数时c ++分段错误

时间:2014-04-17 19:00:03

标签: c++ polymorphism virtual

我正在用C ++进行OOP练习。我有一个名为WARRIOR的课程。它派生出ICEMAN,NINJA,DRAGON,WOLF,LION。在WARRIOR类中。

#ifndef WARRIOR_H
#define WARRIOR_H
#include "camp.h"
#include "weapon.h"
#include <string>
#include <iostream>
#include <vector>
using namespace std;

extern int R;
extern int K;
class Weapon;
class Camp;
class Warrior
{
private:
  string warriorName;
  int warriorHealth;
  int warriorAttack;
  Camp* belongTo;

public:
  int pos;
  int warNo;
  void UseArrow(Warrior *w, Weapon *wp);
  void UseBomb(Warrior *w, Weapon *wp); 
  void UseSword(Warrior *w, Weapon *wp);
  void BrokeWeapon();
  void FightBack(Warrior *w, Weapon *wp);
  //void CopyWeapon(Weapon* wp);

  vector<Weapon*>weapList;
  Warrior(string s, int h, int att, Camp* c)
    :warriorName(s), warriorHealth(h),
    warriorAttack(att), belongTo(c),
    pos(0){}

  void SetHealth(int h){warriorHealth += h;}
  void SetAttack(int h){warriorAttack += h;}

  int WarriorHealth(){return warriorHealth;}
  int WarriorAttack(){return warriorAttack;}
    string GetName(){return warriorName;}
  void Equip(string s);

  bool Is_alive(){return warriorHealth > 0;}

  virtual int GetSpecial() = 0;
  virtual void March() = 0;

};

class Dragon : public Warrior
{
  double dragonMorale;
public:
  Dragon(int m, int h, int att, Camp* c, 
      string s = "dragon")
    :Warrior(s, h, att, c), dragonMorale(m){}
  ///
  void DragonRoar();
  void ChangeMorale(bool win);


  int GetSpecial(){return dragonMorale;}
  void March(){};
};

class Ninja : public Warrior
{
public:
    Ninja(int h, int att, Camp* c, 
      string s = "ninja")
    :Warrior(s, h, att, c){}

  virtual int GetSpecial(){}
  virtual void March(){}
};

class Iceman : public Warrior
{
public:
    Iceman(int h, int att, Camp* c, 
      string s = "iceman")
    :Warrior(s, h, att, c){}

  virtual int GetSpecial(){};
  virtual void March();
};

class Lion : public Warrior
{
int lionLoyalty;
public:
Lion(int loy, int h, int att, Camp* c,
      string s = "lion")
    :Warrior(s, h, att, c), lionLoyalty(loy){};
      //std::cout << WarriorHealth() <<"heal\n";}
void LoseLoyal();
void TransLife(Warrior *w);

  int GetSpecial(){return lionLoyalty;}
  virtual void March(){}
};

class Wolf : public Warrior
{
public:
Wolf(int h, int att, Camp* c,
      string s = "Lion")
    :Warrior(s, h, att, c){}
void Disarm(Warrior* w);
virtual int GetSpecial(){};

virtual void March(){}
};
#endif

void Iceman::March()
{
int temp = WarriorHealth();

if(temp < 10)
  SetHealth(-temp + 1);
else
  SetHealth(-9);

SetAttack(20);
}

我的主要职能:

#include "camp.h"
#include "warrior.h"
#include "weapon.h"
#include "city.h"

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>

using namespace std;

int R;
int K;
int N;

int main()
{

    Warrior* ptr = new Iceman(10, 10, NULL);
    ptr->March();//segmentation fault here

    return 0;
}

然后,我通过gdb运行程序。

Starting program: /home/linzhe/Working/cW/oop/8wk/wow/wow 
warning: no loadable sections found in added symbol-file 
system-supplied DSO 0x7ffff7ffa000

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00000000004063bf in main ()
(gdb) 

似乎是另一个虚拟功能

virtual int GetSpecial();

工作得很好但不是

virtual void March();

请提前帮助和感谢。我使用g ++在Ubuntu 12.04下编译程序。

//////

根据gdb,编译器是否无法找到确切的虚函数?

///// 今天早上,我试图将[warrior.cpp camp.cpp weapon.cpp city.cpp]复制到另一个文件夹,并编写了一个新的main函数来调用Warrior :: March(),它有效。但是,它只是在我的原始代码中不起作用......

1 个答案:

答案 0 :(得分:0)

您对Iceman构造函数的调用无效:

Warrior* ptr = new Iceman(10, 10, 10, NULL); // Signature is (int, int, Camp*, [string])

因此,您实际上将{10}作为Camp对象的地址传递,并NULL传递string

如果 - 例如 - 您将对此构造函数的调用更改为

Warrior* ptr = new Iceman(10, 10, NULL);

它应该有用。