1

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!

itsmepetey
  • 19
  • 1
  • 5
  • 1
    What errors ate you getting? Due to the way templates work, generally you need to put the template definition in the header file as well. – Chad Feb 02 '14 at 22:11
  • Making the members virtual still requires an implementation. [Pure virtual](http://en.cppreference.com/book/intro/abstract_classes#Pure_virtual) is what you're probably after. – gotopie Feb 02 '14 at 22:15
  • 1
    Concerning terminology, it is "class template", not "template class". A "class template" is not a class, it is a template for making classes. – juanchopanza Feb 02 '14 at 22:29

3 Answers3

1

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.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • 1
    The other mistake is to think everything requires interfaces. Oh, and "`using namespace std`". And putting the class template implementation in a `.cpp` file. – juanchopanza Feb 02 '14 at 22:21
  • Ok, So I switched my code to be as you stated Paul but I get an error: undefined reference to `sorted::sorted()'| – itsmepetey Feb 02 '14 at 22:33
  • What is the error? Did you read juanchopanza's comments? More than likely, it is a linker error, not a compiler error. If so, please review the comments made by juanchopanza with respect to not creating a .cpp file for template implementation (and thereby compiling the .cpp file as a separate module). – PaulMcKenzie Feb 02 '14 at 22:34
  • So if I understand correctly, you are saying to put the sorted.h and the sorted.cpp implementation in the same file? – itsmepetey Feb 02 '14 at 22:41
  • @itsmepetey - Please look here: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – PaulMcKenzie Feb 02 '14 at 22:45
  • That link helped greatly with explaining why it needs to be done! Thanks @paulMcKenzie. I guess I will have to do some research on how to do them both in the same file because that is still a little fuzzy as far as the syntax aspect. – itsmepetey Feb 02 '14 at 22:56
1

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.

gotopie
  • 2,594
  • 1
  • 20
  • 23
  • Thank you for your response. A few things are still confusing me: 1.) so I should implement all the functions in the sorted.h file rather than in the sorted.cpp? 2.) and so in the sortedInterface should I not have anything inside the arguements area for each function, but rather just set it = 0? – itsmepetey Feb 02 '14 at 22:45
0

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