"undefined reference to ... "?

This is a portion of my Ring implementation file (Ring.cpp):

Ring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "ring.h"
#include <stdexcept> 

template<class T>
Ring<T>::Ring(): n(DEFAULT_RING_SIZE), elementCount(0),referenceIndex(0) {
  
  // allocate memory for the dynamic arrays:
  elements = new T[n];
  isElementStored = new bool[n];
  
  // make sure ring is cleared
  clearRing();
}

template<class T>
Ring<T>::Ring(int ringSize): n(ringSize), elementCount(0),referenceIndex(0) {
  
  // sanity checking ring size
  if( ringSize < 1 ) {
    n = DEFAULT_RING_SIZE;
  }
  
  // allocate memory
  elements = new T[n];
  isElementStored = new bool[n];
  
  // make sure ring is cleared
  clearRing();
}
...


This is a portion of my Ring header (ring.h) file:

Ring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef _ring_h_
#define _ring_h_

template<class T>
class Ring {
private:
  static const int 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;

...


... and a portion of my main.cpp:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "ring.h"
#include <iostream>
#include <stdexcept> 
#include <cassert>
#include <string>

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

Last edited on
Normally when dealing with templates you need to have both the definition and the implementation in the same file, usually the header.
Topic archived. No new replies allowed.