鱼C论坛

 找回密码
 立即注册
查看: 2345|回复: 3

[已解决]求助,C++继承基类

[复制链接]
发表于 2021-4-28 21:13:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
具体问题:
定义Chess基类,设置虚析构

编译错误显示如下:
undefined reference to `vtable for Chess'
[Error] ld returned 1 exit status

代码如下:
class Chess
{
private:
    int id;
    char* name;
public:
    Chess(int i,char*n)
    {
        id = i;
        name = new char[2];
        strcpy(name,n);
    }
    virtual ~Chess()
    {
        delete name;
    }
    virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery);
};

自我感觉析构已经定义清楚了,但就是不成功,求大佬帮帮我
最佳答案
2021-4-28 21:41:30
  1. #include <iostream>
  2. #include<cmath>
  3. #include<cstring>

  4. using namespace std;

  5. class Board;

  6. class Chess
  7. {
  8. private:
  9.     int id;
  10.     char* name;
  11. public:
  12.     //Chess(int i,char*n)
  13.     Chess(int i, const char *n)
  14.     {
  15.         id = i;
  16.         name = new char[2];
  17.         strcpy(name,n);
  18.     }
  19.     virtual ~Chess()
  20.         {
  21.             delete name;
  22.         }
  23.       
  24.     int getId()
  25.     {
  26.         return id;
  27.     }
  28.     void print()
  29.     {
  30.         cout <<name;
  31.     }
  32.     virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery) {return false;}
  33. };

  34. class C:public Chess
  35. {
  36.         public:
  37.                 //C(int i,char* n):Chess(i,n) {}
  38.                 C(int i, const char *n):Chess(i,n) {}
  39.                 ~C()
  40.                 {
  41.                         cout <<"please";
  42.                 }
  43.                 void show()
  44.                 {
  45.                         cout <<"please";
  46.                 }

  47.                 virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery) {return false;}

  48. };

  49. int main()
  50. {
  51.     C c1(1,"xiang");
  52.     c1.show();
  53.     return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-28 21:19:33 | 显示全部楼层
代码要发完整
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-28 21:23:31 | 显示全部楼层

主要有点长,是个大作业,大部分估计和他没关系....
我按照思路做了个简化版,报错和之前一样

#include <iostream>
#include<cmath>
#include<cstring>

using namespace std;

class Board;

class Chess
{
private:
    int id;
    char* name;
public:
    Chess(int i,char*n)
    {
        id = i;
        name = new char[2];
        strcpy(name,n);
    }
    virtual ~Chess()
        {
            delete name;
        }
       
    int getId()
    {
        return id;
    }
    void print()
    {
        cout <<name;
    }
    virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery);
};

class C:public Chess
{
        public:
                C(int i,char* n):Chess(i,n) {}
                ~C()
                {
                        cout <<"please";
                }
                void show()
                {
                        cout <<"please";
                }

};

int main()
{
    C c1(1,"xiang");
    c1.show();
    return 0;
}

求求大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-28 21:41:30 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include<cmath>
  3. #include<cstring>

  4. using namespace std;

  5. class Board;

  6. class Chess
  7. {
  8. private:
  9.     int id;
  10.     char* name;
  11. public:
  12.     //Chess(int i,char*n)
  13.     Chess(int i, const char *n)
  14.     {
  15.         id = i;
  16.         name = new char[2];
  17.         strcpy(name,n);
  18.     }
  19.     virtual ~Chess()
  20.         {
  21.             delete name;
  22.         }
  23.       
  24.     int getId()
  25.     {
  26.         return id;
  27.     }
  28.     void print()
  29.     {
  30.         cout <<name;
  31.     }
  32.     virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery) {return false;}
  33. };

  34. class C:public Chess
  35. {
  36.         public:
  37.                 //C(int i,char* n):Chess(i,n) {}
  38.                 C(int i, const char *n):Chess(i,n) {}
  39.                 ~C()
  40.                 {
  41.                         cout <<"please";
  42.                 }
  43.                 void show()
  44.                 {
  45.                         cout <<"please";
  46.                 }

  47.                 virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery) {return false;}

  48. };

  49. int main()
  50. {
  51.     C c1(1,"xiang");
  52.     c1.show();
  53.     return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-15 17:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表