[英]Implementing a template class interface
I am relatively new to c++ and am having a heck of a time getting my main program to instantiate my class. 我对c ++相对较新,并且有一段时间让我的主程序实例化我的类。 I am used to java so I'm not sure if I am mixing up the two languages as I attempt to do this and that is my problem or maybe I just don't understand the concept correctly. 我习惯于使用Java,所以我不确定在尝试这样做时是否要混用这两种语言,这就是我的问题,或者我只是不正确地理解这一概念。
The object of my program: The object of this program is to create a template class from an interface that will make a sorted array that you can add and remove items from it while keeping it sorted. 我程序的目标:该程序的目标是从一个接口创建一个模板类,该接口将创建一个排序后的数组,您可以在其中排序的同时添加和删除项目。
Note: Please help me actually understand this process as to just telling me the exact code to use because I really want to understand what I am doing wrong for next time. 注意:请帮助我真正了解此过程,只是告诉我要使用的确切代码,因为我真的很想了解下一次我做错了什么。
Step 1: I created my sorted interface: 步骤1:我创建了排序后的界面:
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
then I used the interface to create the sorted.h file: 然后我使用该接口来创建sorted.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
and finally I created the sorted.cpp (I only included the constructor for now as I can't even get that working) 最后我创建了sorted.cpp(我现在仅包括构造函数,因为我什至无法正常工作)
#include "sorted.h"
#include <iostream>
using namespace std;
template<class ListItemType>
sorted<ListItemType>::sorted()
{
itemCount = 0;
items[DEFAULT_BAG_SIZE];
maxItems = DEFAULT_BAG_SIZE;
}
My main program: 我的主程序:
#include "sortedInterface.h"
#include "sorted.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
sorted<string> sorted1 = new sorted();
return 0;
};
Any help is appreciated in explaining where my logic is failing on this and any hints on how to properly execute my task. 感谢您在解释我的逻辑失败的地方以及有关如何正确执行任务的任何提示方面的帮助。 Thanks! 谢谢!
1) operator "new" returns a pointer, not an object. 1)运算符“ new”返回一个指针,而不是对象。
sorted<string>* sorted1 = new sorted<string>();
2) However, in your small example, there is no need to create sorted1 using "new". 2)但是,在您的小示例中,无需使用“ new”创建sorted1。
sorted<string> sorted1;
One word of advice -- Java is not C++. 忠告一词-Java不是C ++。 You made the two mistakes that many first-time Java programmers make when writing C++ code, namely 1) believing that to create an object, you must use "new", and 2), that "new" returns a reference. 您犯了许多首次Java程序员在编写C ++代码时犯的两个错误,即1)认为创建对象必须使用“ new”,以及2)“ new”返回引用。
There are a few things wrong with your interface/implementation. 您的界面/实现有些问题。 A class template is usually implemented entirely in the header in which it's declared; 类模板通常完全在声明它的标头中实现; this is because the compiler creates a whole new type for each type you use with your template. 这是因为编译器会为您与模板一起使用的每种类型创建一个全新的类型。
Second, in your sortedInterface
template, you've made the members virtual which still requires a definition, but you do not supply one. 其次,在您的sortedInterface
模板中,已将成员虚拟化,该成员仍需要一个定义,但不提供定义。 You can mark your member functions with = 0;
您可以将成员函数标记为= 0;
to make them all pure virtual , which means the classes that inherit your sortedInterface
will have to implement those members instead. 为了使它们全部都是纯虚拟的 ,这意味着继承您sortedInterface
的类将必须实现这些成员。
Third, as PaulMcKenzie pointed out, operator new()
returns a pointer to a heap-allocated object, but you're expecting a value type. 第三,正如PaulMcKenzie指出的那样, operator new()
返回指向堆分配对象的指针,但是您期望使用值类型。
Finally, please take a look at smart pointers if you're using naked new()
s everywhere. 最后,如果您到处都在使用裸new()
,请看一下智能指针 。
I notice the following additional anomalies in the entire implementation: 我注意到整个实现中存在以下其他异常情况:
class Sorted
does not inherit from the so-called interface sortedInterface
class Sorted
不继承于所谓的sortedInterface
接口 class Sorted
您尚未在class Sorted
定义构造函数的所有版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.