【问题标题】:Implementing a template class interface实现模板类接口
【发布时间】:2014-02-02 22:03:59
【问题描述】:

我对 c++ 比较陌生,并且有一段时间让我的主程序实例化我的类。我习惯了 java,所以我不确定我是否在尝试这样做时混淆了这两种语言,这是我的问题,或者我只是没有正确理解这个概念。

我的程序的对象: 这个程序的对象是从一个接口创建一个模板类,该接口将生成一个排序数组,您可以在其中添加和删除项目,同时保持排序。

注意:请帮助我真正理解这个过程,只告诉我要使用的确切代码,因为我真的很想了解下一次我做错了什么.

第 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

然后我使用接口创建了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

最后我创建了 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;
}

我的主程序:

#include "sortedInterface.h"
#include "sorted.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    sorted<string> sorted1 = new sorted();

    return 0;
};

感谢任何帮助解释我的逻辑在哪里失败以及如何正确执行我的任务的任何提示。谢谢!

【问题讨论】:

  • 您遇到了什么错误?由于模板的工作方式,通常您还需要将模板定义放在头文件中。
  • 使成员虚拟化仍然需要一个实现。 Pure virtual 可能是您所追求的。
  • 关于术语,它是“类模板”,而不是“模板类”。 “类模板”不是类,它是制作类的模板。

标签: c++ class templates interface


【解决方案1】:

1) 运算符“new”返回一个指针,而不是一个对象。

sorted<string>* sorted1 = new sorted<string>();

2) 但是,在您的小示例中,无需使用“new”创建 sorted1。

sorted<string> sorted1;

一句忠告——Java 不是 C++。您犯了许多初学 Java 程序员在编写 C++ 代码时犯的两个错误,即 1) 认为创建对象必须使用“new”,以及 2) 认为“new”返回引用。

【讨论】:

  • 另一个错误是认为一切都需要接口。哦,还有“using namespace std”。并将类模板实现放在.cpp 文件中。
  • 好的,所以我将我的代码切换为你所说的 Paul 但我收到一个错误:未定义引用 `sorted<:string>::sorted()'|
  • 错误是什么?你读过 juanchopanza 的 cmets 吗?这很可能是链接器错误,而不是编译器错误。如果是这样,请查看 juanchopanza 制作的关于不为模板实现创建 .cpp 文件(从而将 .cpp 文件编译为单独的模块)的 cmets。
  • 所以如果我理解正确的话,你是说将 sorted.h 和 sorted.cpp 实现放在同一个文件中?
  • @itsmepetey - 请看这里:stackoverflow.com/questions/495021/…
【解决方案2】:

您的界面/实现存在一些问题。类模板通常完全在声明它的头文件中实现;这是因为编译器会为您在模板中使用的每种类型创建一个全新的类型。

其次,在您的sortedInterface 模板中,您已将成员设为虚拟,这仍然需要定义,但您没有提供定义。您可以使用= 0; 标记您的成员函数,使其全部为pure virtual,这意味着继承您的sortedInterface 的类将不得不实现这些成员。

第三,正如 PaulMcKenzie 指出的那样,operator new() 返回一个指向堆分配对象的指针,但您需要一个值类型。

最后,如果你到处都在使用裸new()s,请看看smart pointers

【讨论】:

  • 感谢您的回复。一些事情仍然让我感到困惑:1.)所以我应该在 sorted.h 文件中而不是在 sorted.cpp 中实现所有功能? 2.) 所以在 sortedInterface 中我是否应该在每个函数的争论区域内没有任何东西,而只是将它设置为 = 0?
【解决方案3】:

我注意到整个实现中存在以下额外异常:

  • 接口应该是不可实例化的,但它是 在您的情况下可实例化(因为甚至没有一个纯
    您所谓的接口中的虚函数)标准规则是
    使接口中的所有功能成为纯虚拟(=0)
  • class Sorted不继承所谓的接口 sortedInterface
  • 您尚未在class Sorted 中定义所有版本的构造函数
  • 如果您希望多态性发挥作用(具体的接口),您
    需要在接口和
    中都有虚拟类析构函数 具体类

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 2015-06-07
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2020-09-08
    • 2016-07-22
    相关资源
    最近更新 更多