0

Edit: This was not an issue with the template not being defined in the header. This error was caused by not defining a copy constructor.

I tried googling this but I couldn't find anything except for issues with people calling functions without the proper parameters and things along those lines. So, here's a very small chunk from main. I have created a templated container class and initialized test as one to hold strings. sorted.h is included in both .cpp files.

Error here: https://i.stack.imgur.com/uGeeZ.jpg

  // Test assignment operator
  // Can test copy constructor by changing following two lines to
  //   sorted<string> test(songs);
  sorted<string> test;
  test = songs; // Error occurs when commenting this line back in.

Here is my overloaded assignment operator. I assume this is what's causing the problem since commenting the line that would use it is what causes the linker error. Anyone have an idea what I'm doing wrong here?

//Overloaded assignment operator. (sorted.cpp)
template <class T>
sorted<T> sorted<T>::operator=(const sorted<T>& srtd){

  if (this != &srtd){
    delete [] m_data;
    delete [] m_randfinal;

    m_data = new T[srtd.m_capacity];
    m_randfinal = new T[srtd.m_size];
    m_size = srtd.m_size;
    m_capacity = srtd.m_capacity;   

    for (int i = 0; i < m_size; i++){
      m_data[i] = srtd.m_data[i];
      m_randfinal[i] = srtd.m_randfinal[i];
    }
  }

  return *this;
} 

Declaration in sorted.h.

// Overloaded assignment operator
sorted<T> operator=(const sorted<T>& srtd);
TyManning
  • 125
  • 12
  • 1
    Implementation of templated classes must be located in the headers in order for the compiler to properly instantiate them. – Daniel Kamil Kozar May 05 '16 at 21:58
  • Yeah, we were taught to include the cpp files at the bottom of the header to avoid that. Professor said it's not technically a good practice but it gets around it for the time being. Everything else in my program is working at this point, though. – TyManning May 05 '16 at 21:59
  • I tried moving it over to the header file, but no dice. – TyManning May 05 '16 at 22:06
  • 2
    The error message seems to suggest that you're missing a copy constructor for your class. Did you create one, or did you create any other constructors (which would disable the implicit copy constructor)? – Daniel Kamil Kozar May 05 '16 at 22:09
  • Ah. It would appear that I might not have implemented it just yet. And right you are. Thanks! – TyManning May 05 '16 at 22:12
  • Would you please add this as a solution and I'll approve? Thanks again! – TyManning May 05 '16 at 22:20

1 Answers1

0

The linker error message suggests that the code needs to call a copy constructor, but cannot find it. If you defined any constructors yourself, keep in mind that the implicit copy constructor will be disabled, and you will need to define one yourself.

Also, remember that the implementation of templated classes must be located in the headers in order for the compiler to properly instantiate them.

Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64