-1

I had a redefinition problem but I noticed that I already included .cpp file in the .hpp file so my mistake was including the .hpp file in my .cpp file again Now I am getting this error, something to do with templates. Also while you fix my problem, can you explain to me what template class does? cplusplus.com is not that descriptive. Thank you. :)

//implementation
template<class T>
ArrayBag<T>::ArrayBag() : item_count_(0){}

-------------WARNING YOU ARE NOW LEAVING IMPLEMENTATION---------------------------

//interface
    #ifndef ARRAY_BAG_H
#define ARRAY_BAG_H
#include <vector>

template<class T>
class ArrayBag
{
    protected:
        static const int DEFAULT_CAPACITY = 200;
        T items_[DEFAULT_CAPACITY];
        int item_count_;
        int get_index_of_(const T& target) const;
    public:
        ArrayBag();
        int getCurrentSize() const;
        bool isEmpty() const;
        //adds a new element to the end, returns true if it was successfully been added
        bool add(const T& new_entry);
        bool remove(const T& an_entry);
        void clear();
        bool contains(const T& an_entry) const;
        int getFrequencyOf(const T& an_entry) const;
        std::vector<T> toVector() const;
        void display() const;
        //overloading operators for objects
        void operator+=(const ArrayBag<T>& a_bag);
        void operator-=(const ArrayBag<T>& a_bag);
        void operator/=(const ArrayBag<T>& a_bag);
};

#include "ArrayBag.cpp"
#endif

-------------WARNING YOU ARE NOW LEAVING INTERFACE---------------------------

//error
5 C:\Users\minahnoona\Desktop\ArrayBag.cpp expected constructor, destructor, or type conversion before '<' token 
5 C:\Users\minahnoona\Desktop\ArrayBag.cpp expected `;' before '<' token 
Advancedip
  • 39
  • 2
  • 4

1 Answers1

2

Don't call your ArrayBag.cpp a .cpp file. Template implementations go in header files, and the name should reflect that.

If you want the implementation in a separate file (you don't strictly need to), call it ipp or tpp. Something the project system won't try to compile on its own.

Then include it from the .hpp and don't include the .hpp from the .ipp.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • You could've mentioned that that the cpp file is separately compiled and causes trouble. – Red.Wave Sep 21 '19 at 15:19
  • @Red.Wave -- I don't see anything in the question that suggests that the cpp file is being compiled separately. But if that's the case, since it has only the definition of a template function, it's harmless. – Pete Becker Sep 21 '19 at 15:49