#ifndef _ring_h_
#define _ring_h_
template<class T>
class Ring {
private:
staticconstint DEFAULT_RING_SIZE = 10;
int n; // this is the ring's size as noted above
int elementCount; // number of elements actually stored
T* elements; // dynamic array that holds the actual elements
bool* isElementStored; // dynamic array that keeps tab whether element is stored
int referenceIndex; // index to the dynamic arrays
public:
/**
* Default constructor.
* @post a ring of size DEFAULT_RING_SIZE (10) created.
* there are no elements and reference is at the beginning
*/
Ring();
/**
* Parameterized constructor.
* @param ringSize size of the new ring
* @post a ring of size ringSize is created.
* if ringSize is an invalid size, it is set to DEFAULT_RING_SIZE,
* there are no elements, and reference is at the beginning
*/
Ring(int ringSize);
/**
* Stores a value at the current reference
* @param value a value of type T
* @post
* if the value is non-null, it is stored at the current reference
* if the value is null, the element at reference is effectively removed
* if there was no previous value at the current reference, the
* number of elements is increased by 1
*/
void set(const T& value);
/**
* Retrieves the value stored at current reference
* @post no internal state is modified
* @return value stored at current reference
* if no values were stored at current reference, returns NULL
*/
T get() const;
...
#include "ring.h"
#include <iostream>
#include <stdexcept>
#include <cassert>
#include <string>
usingnamespace std;
int main() {
// create a ring of strings of size 7 to hold days of the week
Ring<string> days(7);
assert(days.getRingSize()==7);
assert(days.getElementCount()==0);
// populate the ring
days.set("Monday");
days.moveNext();
days.set("Tuesday");
days.moveNext();
days.set("Wednesday");
days.moveNext();
days.set("Thursday");
days.moveNext();
days.set("Friday");
days.moveNext();
days.set("Saturday");
days.moveNext();
days.set("Sunday");
assert(days.getElementCount()==7);
days.remove();
assert(days.getElementCount()==6);
try {
cout << days.get() << endl;
} catch(std::runtime_error e) {
cerr << "An exception was intentionally generated, and handled" << endl;
}
...
Sorry, I omitted most of the code for each file but anytime I run g++ Ring.cpp main.cpp -o ringexec.exe in my console, I'm getting errors similar to this: "undefined reference to 'Ring<std::string>::Ring(int)".
What I don't understand is why I'm getting this error? Is main.cpp not recognizing the header file? I know that C++ is case sensitive but everything matches, unless I'm missing something.