实现模板类接口 P>我对C++是比较新的,而且我有一段时间让我的主程序实例化我的类。我已经习惯了java,所以我不确定在我尝试这样做时是否混淆了这两种语言,这是我的问题,或者我只是没有正确理解这个概念
我的程序的对象:此程序的对象是从一个接口创建一个模板类,该类将生成一个排序数组,您可以在保持排序的同时添加和删除其中的项 注意:请帮助我真正理解这个过程,就像告诉我要使用的确切代码一样,因为我真的想知道下次我做错了什么 步骤1:我创建了我的排序界面:实现模板类接口 P>我对C++是比较新的,而且我有一段时间让我的主程序实例化我的类。我已经习惯了java,所以我不确定在我尝试这样做时是否混淆了这两种语言,这是我的问题,或者我只是没有正确理解这个概念,c++,class,templates,interface,C++,Class,Templates,Interface,我的程序的对象:此程序的对象是从一个接口创建一个模板类,该类将生成一个排序数组,您可以在保持排序的同时添加和删除其中的项 注意:请帮助我真正理解这个过程,就像告诉我要使用的确切代码一样,因为我真的想知道下次我做错了什么 步骤1:我创建了我的排序界面: sortedInterface.h #ifndef _SORTED_INTERFACE #define _SORTED_INTERFACE #include <vector> using namespace std; templat
sortedInterface.h
#ifndef _SORTED_INTERFACE
#define _SORTED_INTERFACE
#include <vector>
using namespace std;
template<class ListItemType>
class sortedInterface
{
public:
virtual bool sortedIsEmpty();
virtual int sortedGetLength();
virtual bool sortedInsert(ListItemType newItem);
virtual bool sortedRemove(ListItemType anItem);
virtual bool sortedRetrieve(int index, ListItemType dataItem);
virtual int locatePosition(ListItemType anItem);
}; // end SortedInterface
#endif
sortedInterface.h
#ifndef\u排序\u接口
#定义_排序_接口
#包括
使用名称空间std;
模板
类分类接口
{
公众:
虚拟布尔排序空();
虚拟int-sortedGetLength();
虚拟bool-sortedInsert(ListItemType-newItem);
虚拟bool-sortedRemove(ListItemType-anItem);
虚拟bool-sortedRetrieve(int索引,ListItemType数据项);
虚拟int位置(ListItemType anItem);
}; // 端面
#恩迪夫
然后,我使用该接口创建排序的.h文件:
sorted.h
#include "sortedInterface.h"
#include <iostream>
#ifndef SORTED_H
#define SORTED_H
using namespace std;
template<class ListItemType>
class sorted
{
public:
sorted();
sorted(int i);
bool sortedIsEmpty();
int sortedGetLength();
bool sortedInsert(ListItemType newItem);
bool sortedRemove(ListItemType anItem);
bool sortedRetrieve(int index, ListItemType dataItem);
int locatePosition(ListItemType anItem);
protected:
private:
const int DEFAULT_BAG_SIZE = 10;
ListItemType items[];
int itemCount;
int maxItems;
};
#endif // SORTED_H
sorted.h
#包括“sortedInterface.h”
#包括
#ifndef已分类
#定义排序的
使用名称空间std;
模板
分类
{
公众:
排序();
分类(int i);
bool-disempty();
int sortedGetLength();
bool-sortedInsert(ListItemType-newItem);
bool sortedRemove(列表项类型anItem);
bool sortedRetrieve(int索引,ListItemType数据项);
int locatePosition(列表项类型anItem);
受保护的:
私人:
const int DEFAULT_BAG_SIZE=10;
ListItemType项目[];
整数项计数;
整数最大项;
};
#endif//SORTED_H
最后,我创建了sorted.cpp(我现在只包括构造函数,因为我甚至不能让它工作)
#包括“sorted.h”
#包括
使用名称空间std;
模板
排序的::排序的()
{
itemCount=0;
项目[默认袋大小];
maxItems=默认的行李大小;
}
我的主要节目:
#include "sortedInterface.h"
#include "sorted.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
sorted<string> sorted1 = new sorted();
return 0;
};
#包括“sortedInterface.h”
#包括“sorted.h”
#包括
#包括
使用名称空间std;
int main()
{
排序的sorted1=新排序的();
返回0;
};
如果您能帮助我解释我的逻辑在这方面的缺陷,以及如何正确执行任务的提示,我们将不胜感激。谢谢 1)运算符“new”返回指针,而不是对象
sorted<string>* sorted1 = new sorted<string>();
sorted*sorted1=新排序();
2) 但是,在您的小示例中,不需要使用“new”创建sorted1
1;
一个忠告——java不是C++。在编写C++代码时,你犯了许多第一次java程序员所犯的两个错误,即1)相信要创建一个对象,你必须使用“新”,2),“新”返回引用。
你的接口/实现有一些错误。类模板通常完全在声明它的头中实现;这是因为编译器会为模板使用的每种类型创建一个全新的类型
其次,在您的sortedInterface
模板中,您已经将成员设置为虚拟的,这仍然需要定义,但您没有提供定义。您可以使用=0标记您的成员函数
将它们全部生成,这意味着继承sortedInterface
的类将必须实现这些成员
第三,正如PaulMcKenzie指出的,操作符new()
返回一个指向堆分配对象的指针,但您需要一个值类型
最后,请查看您是否在所有地方都使用裸
new()
s。我注意到在整个实现中还有以下异常:
- 接口应该是不可实例化的,但它是
在您的情况下是可实例化的(因为甚至没有一个纯的
所谓接口中的虚拟功能)标准规则是 使界面中的所有函数纯虚拟(=0)
不会从所谓的接口继承类排序
sortedInterface
- 您尚未在
类中定义构造函数的所有版本 已排序
- 如果您想让多态性发挥作用(与具体的接口),您可以
在接口和中都需要虚拟类析构函数
混凝土类
使用命名空间std
”。然后将类模板实现放在.cpp
文件中。好的,所以我将代码切换为您所说的那样,但我得到一个错误:未定义对'sorted::sorted()'的引用;错误是什么?你读过胡安科潘扎的评论吗?很可能是链接器错误,而不是编译器错误。如果是,请查看juanchopanza关于不创建用于模板实现的.cpp文件(从而将.cpp文件编译为单独的模块)的评论。如果我理解正确,您是说将sorted.h和sorted.cpp实现放在同一个文件中?@itsmepetey-请看这里:谢谢您的回复。有几件事仍然让我感到困惑:1)那么我应该在sorted.h文件中实现所有函数,而不是在sorted.cpp中?2.)所以在分类界面中,我应该没有任何东西在里面吗
sorted<string>* sorted1 = new sorted<string>();
sorted<string> sorted1;