简体   繁体   中英

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. 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.

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:

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
#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)

#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.

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

2) However, in your small example, there is no need to create sorted1 using "new".

sorted<string> sorted1;

One word of advice -- Java is not 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.

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. You can mark your member functions with = 0; to make them all pure virtual , which means the classes that inherit your sortedInterface will have to implement those members instead.

Third, as PaulMcKenzie pointed out, operator new() returns a pointer to a heap-allocated object, but you're expecting a value type.

Finally, please take a look at smart pointers if you're using naked new() s everywhere.

I notice the following additional anomalies in the entire implementation:

  • An interface is something which should be non-instantiable but it is instantiable in your case (because there is not even a single pure
    virtual function in your so called interface) Standard rule is to
    make all the functions in the interface pure virtual (=0)
  • class Sorted does not inherit from the so-called interface sortedInterface
  • You have not defined all versions of your constructor in your class Sorted
  • If you want the polymorphism to work (Interface to Concrete), you
    need to have virtual class destructors in both the interface and
    concrete class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM