鱼C论坛

 找回密码
 立即注册
查看: 1986|回复: 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
#include <iostream>
#include<cmath>
#include<cstring>

using namespace std;

class Board;

class Chess
{
private:
    int id;
    char* name;
public:
    //Chess(int i,char*n)
    Chess(int i, const 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) {return false;}
};

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

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

};

int main()
{
    C c1(1,"xiang");
    c1.show();
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-28 21:19:33 | 显示全部楼层
代码要发完整
想知道小甲鱼最近在做啥?请访问 -> 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;
}

求求大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

using namespace std;

class Board;

class Chess
{
private:
    int id;
    char* name;
public:
    //Chess(int i,char*n)
    Chess(int i, const 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) {return false;}
};

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

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

};

int main()
{
    C c1(1,"xiang");
    c1.show();
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 13:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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