0

This is a excerpt from my header file

template <typename E>
class Container {
public:
  Container& operator=(const Container&) = delete;
  Container(const Container&) = delete;
  Container() = default;

  virtual ~Container() { }
...

Then I've created another header file to declare a new class and to implement the methods (these are not shown now)

template <typename E>
class SepChaining : public Container<E> {
  size_t nmax;
  size_t n;
  int *values;
public:

  SepChaining<E>( size_t nmax ) : nmax(nmax), n(0), values( new int[this->nmax]) { }

  virtual ~SepChaining<E>( );

Here I've created a new class SepChaining that inherits the Container class and I created a constructor to assign the nmax and values

Now my question: How do I create a new instance of this class? I am really confused with where I need to specify the actual template value, say int

EDIT:

Header.h

 #include <iostream>
 #include <functional>

enum Order { dontcare, ascending, descending };

template <typename E>
class Container {
public:
  Container& operator=(const Container&) = delete;
  Container(const Container&) = delete;
  Container() = default;

  virtual ~Container() { }

  virtual void add(const E& e) { add(&e, 1); }
  virtual void add(const E e[], size_t s) = 0;

  virtual void remove(const E& e) { remove(&e, 1); }
  virtual void remove(const E e[], size_t s) = 0;

  virtual bool member(const E& e) const = 0;
};

SepChaining.h

 #include <iostream>
 #include "Container.h"

template <typename E>
class SepChaining : public Container<E> {
    size_t nmax;
    size_t n;
    int *values;
public:

    SepChaining<E> ( size_t nmax ) : nmax(nmax), n(0), values(new int[this->nmax]) { }

    virtual ~SepChaining ();

    using Container<E>::add;
    virtual void add(const E e[], size_t s);

    using Container<E>::remove;
    virtual void remove(const E e[], size_t s);

    virtual bool member(const E& e) const;

};

SepChaining.cpp

#include <iostream>
#include "SepChaining.h"

template <typename E>
SepChaining<E>::~SepChaining( ){
    delete[] values;
}

template <typename E>
void SepChaining<E>::add(const E e[], size_t s) {
    std::cout << "Add method";
}

template <typename E>
void SepChaining<E>::remove(const E e[], size_t s) {
    std::cout << "Remove method";
}

template <typename E>
bool SepChaining<E>::member(const E &e) const {
    for (size_t i = 0; i < n; ++i) {
        if (values[i] == e) return true;
    }
    return false;
}

So these are my three files, the main.cpp is just initialising of the constructor as you told me. I can't see any problem with my code..

brance
  • 1,157
  • 9
  • 22

1 Answers1

1

Simply change E to int:

SepChaining<int> instance;
erenon
  • 18,838
  • 2
  • 61
  • 93
  • When I try to do it I'm getting the following errors: _main in main.o ld: symbol(s) not found for architecture x86_64 – brance May 18 '15 at 20:13
  • That one is a different issue, solved e.g here: http://stackoverflow.com/questions/11852568/gcc-4-8-on-mac-os-x-10-8-throws-undefined-symbols-for-architecture-x86-64 (you have to call g++ instead of gcc) – erenon May 18 '15 at 20:23
  • I've included the library, but I am getting the same error: Undefined symbols for architecture x86_64: "SepChaining::add(int const*, unsigned long)", referenced from: vtable for SepChaining in main-ce609f.o "SepChaining::~SepChaining()", referenced from: vtable for SepChaining in main-ce609f.o "SepChaining::member(int const&) const", referenced from: vtable for SepChaining in main-ce609f.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – brance May 18 '15 at 21:05
  • This is a different question again. You have to implement the derived destructor: `virtual ~SepChaining( ) {}` – erenon May 18 '15 at 21:06
  • I've edited my original post, I guess it will give you a better overview of what I'm doing. – brance May 18 '15 at 21:18
  • Class template members must go to the header file. http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – erenon May 19 '15 at 20:00