求助,C++继承基类
具体问题:定义Chess基类,设置虚析构
编译错误显示如下:
undefined reference to `vtable for Chess'
ld returned 1 exit status
代码如下:
class Chess
{
private:
int id;
char* name;
public:
Chess(int i,char*n)
{
id = i;
name = new char;
strcpy(name,n);
}
virtual ~Chess()
{
delete name;
}
virtual bool judgeMove(Board &b,int startx,int starty,int afterx,int aftery);
};
自我感觉析构已经定义清楚了,但就是不成功,求大佬帮帮我
代码要发完整
人造人 发表于 2021-4-28 21:19
代码要发完整
主要有点长,是个大作业,大部分估计和他没关系....
我按照思路做了个简化版,报错和之前一样
{:10_266:}
#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;
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;
}
求求大佬 #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;
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;
}
页:
[1]