0
#ifndef SORTEDTYPE_H_INCLUDED
#define SORTEDTYPE_H_INCLUDED

const int MAX_ITEMS = 5;

template <class T>
class SortedType
{
 public :
   SortedType();
   void MakeEmpty();
   bool IsFull();
   int  LengthIs();
   void InsertItem(T);
   void DeleteItem(T);
   void RetrieveItem(T&, bool&);
   void ResetList();
   void GetNextItem(T&);

 private:
  int length;
  T info[MAX_ITEMS];
  int currentPos;
};
#endif // SORTEDTYPE_H_INCLUDED



#include "sortedtype.h"

template <class T>
SortedType<T>::SortedType()
{
 length = 0;
 currentPos = - 1;
}

template <class T>
void SortedType<T>::MakeEmpty()
{
 length = 0;
}

template <class T>
bool SortedType<T>::IsFull()
{
 return (length == MAX_ITEMS);
}

template <class T>
int SortedType<T>::LengthIs()
{
 return length;
}

template <class T>
void SortedType<T>::InsertItem(T item)
{
    int location = 0;
    for(int i= 0; i<length; i++)
    {
        if(info[location]<item)
            location++;
    else
        break;
    }
    for(int i=length; i>location;i--)
    {
        info[i]=info[i-1];
    }
    info[location]= item;
    length++;
}
template <class T>
void SortedType<T>::DeleteItem(T item)
{
    int location=0;
    for(int i=0; i<length; i++)
    {
        if(info[i] == item)
            break;
        location++;
    }
    for(int i=location; i<length; i++)
    {
        info[i] = info[i+1];
    }
    length--;
}

template <class T>
void SortedType<T>::RetrieveItem(T& item, bool &found)
{
    int first=0, last=length-1, mid;
    while(first<=last)
    {
        mid=(first+last)/2;
        if(info[mid]== item)
        {
            found=true;
            return;
        }
        if(info[mid]<item)
        {
            first=mid+1;
        }
        else
        {
            last=mid-1;
        }
    }
    found=false;
}

template <class T>
void SortedType<T>::ResetList()
{
 currentPos = - 1;
}

template <class T>
void SortedType<T>::GetNextItem(T& item)
{
 currentPos++;
 item = info [currentPos];
}

I am getting a redefinition error in the cpp file, can anyone please say, how to fix it? I have tried all the possible things. I am not getting what mistake have I done. After building its just saying "error: redefinition of 'SortedType::SortedType()'" and same thing for all the other functions. What things can remove this error? What changes can be done to fix it. I have posted the code which I have written.

  • https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Cory Kramer Aug 06 '20 at 11:17
  • I'd rather except a redefinition of `MAX_ITEMS`. Can you copy and paste full error message into the question? – Yksisarvinen Aug 06 '20 at 11:20
  • Please provide a [mre], other than templates generally needing to be implemented in the header file (which will produce a different error when it comes to linking your program) your code looks OK and [compiles](https://godbolt.org/z/eGxzfx) – Alan Birtles Aug 06 '20 at 11:20
  • Are you including the .cpp file in another .cpp file? If that somehow happens more than once in some other .cpp file, that might explain your problem. – Peter Aug 06 '20 at 11:30
  • What is the #include "sortedtype.h" doing? What is the name of the file you have shown? if it is "sortedtype.h", you are recursing a header file, which would be very strange construction. – ttemple Aug 06 '20 at 12:28
  • Never mind. I guess you have run the header file and the cpp file together... – ttemple Aug 06 '20 at 12:29
  • This whole class can be put in the .h file (take out the #include "sortedtype.h" and make this whole section of code a .h file) Then include the .h file in the code file(s) where you want to use the class, and it should build fine. I tested in MSVC 17 and it seems to work fine. – ttemple Aug 06 '20 at 14:21

1 Answers1

0

Functions that are defined within a header, have to be inline otherwise you get a redefinition error if they are used in multiple translation units.

If you define a member function within the body of the class then the member function is implicitly marked inline. If you define it outside of the class body, like in your example then you need to mark it inline explicitly, by adding the inline keyword in front of it.

t.niese
  • 39,256
  • 9
  • 74
  • 101