C++ 如何修复错误C2259:无法实例化抽象类
我正在使用虚拟类执行赋值,我需要实现一个纯虚拟方法,该方法稍后将在继承类中实现C++ 如何修复错误C2259:无法实例化抽象类,c++,class,C++,Class,我正在使用虚拟类执行赋值,我需要实现一个纯虚拟方法,该方法稍后将在继承类中实现 class Record { char* artist; char* titlu; int durata; public: Record() { artist = new char[50]; titlu = new char[50]; durata = 0; } void setArtist(char *s)
class Record
{
char* artist;
char* titlu;
int durata;
public:
Record()
{
artist = new char[50];
titlu = new char[50];
durata = 0;
}
void setArtist(char *s)
{
strcpy(artist, s);
}
char* getArtist()
{
return artist;
}
void setTitlu(char* s)
{
strcpy(titlu, s);
}
char* getTitlu()
{
return titlu;
}
void setDurata(int n)
{
int durata = n;
}
int getDurata()
{
return durata;
}
};
class Playlist
{
Record* p; /// "Record" is another class
public:
Playlist(int n)
{
p = new Record[n];
}
virtual void sortare(int n) = 0;
};
class PlaylistImplementation :public Playlist
{
public:
void sortare(int n)
{
if (n == 1)
{
cout << "1";
}
else if (n == 2)
{
cout << "2";
}
else
cout << "3";
}
};
以前,我用工作完美的纯虚拟方法解决了一个问题,通过在继承类中实现方法,我自己解决了现在收到的错误(错误C2259:“播放列表”:无法实例化抽象类)
class Record
{
char* artist;
char* titlu;
int durata;
public:
Record()
{
artist = new char[50];
titlu = new char[50];
durata = 0;
}
void setArtist(char *s)
{
strcpy(artist, s);
}
char* getArtist()
{
return artist;
}
void setTitlu(char* s)
{
strcpy(titlu, s);
}
char* getTitlu()
{
return titlu;
}
void setDurata(int n)
{
int durata = n;
}
int getDurata()
{
return durata;
}
};
class Playlist
{
Record* p; /// "Record" is another class
public:
Playlist(int n)
{
p = new Record[n];
}
virtual void sortare(int n) = 0;
};
class PlaylistImplementation :public Playlist
{
public:
void sortare(int n)
{
if (n == 1)
{
cout << "1";
}
else if (n == 2)
{
cout << "2";
}
else
cout << "3";
}
};
课堂记录
{
char*艺术家;
char*titlu;
内杜拉塔;
公众:
记录()
{
艺术家=新字符[50];
titlu=新字符[50];
杜拉塔=0;
}
无效设置艺术家(字符*s)
{
strcpy(艺术家,s);
}
char*getArtist()
{
回归艺术家;
}
void setTitlu(字符*s)
{
strcpy(titlu,s);
}
char*getTitlu()
{
返回titlu;
}
无效设定值(整数n)
{
int-durata=n;
}
int getDurata()
{
返回杜拉塔;
}
};
类播放列表
{
Record*p;//“Record”是另一个类
公众:
播放列表(int n)
{
p=新记录[n];
}
虚空排序(int n)=0;
};
类播放列表实现:公共播放列表
{
公众:
无效索塔雷(国际北)
{
如果(n==1)
{
不能直接实例化播放列表,因为它是抽象的,所以在main
中,而不是:
Playlist x(n);
你需要
PlaylistImplementation x(n);
在playlimplementation
中需要一个构造函数,如下所示:
PlaylistImplementation (int n) : PlayList (n) { }
类播放列表中记录类型的成员“p”隐式声明为私有。要访问,您需要添加公共成员函数(例如,将数据存储到记录)。记录是否为抽象类?否,它具有曲目名称、艺术家等的“set”和“get”方法,而不是播放列表x(n)
,我想你的意思是播放列表x(n)
。该定义位于继承的类中:PlaylistImplementation@PaulSanders我想使用内存分配创建多个记录实例为什么需要构造函数?否则如何将n
传递给Playlist
构造函数?构造函数实例化的对象播放列表的名称是什么?只需like我打电话“playlix(n)”如何存储要录制的数据?听起来你需要