0

Let me post my code first:

Set.h

#pragma once
#include <string>

 template<class _type> class Set
 {
 public:
    Set();
    Set m_add(Set _set1, Set _set2);
    void m_addElem(Set *const _set, _type _elem); 
    void m_deleteElem(Set *const _set, _type _elem); 
    void m_addArray(_type _arr[], int _size); 
    Set(Set &_coll);
    void operator+(_type _num);
    void operator+(_type _elem[]);
    Set operator+(Set *const _set);
    void operator-(_type _num);
    Set & operator=(Set &_set);
    void m_display();
    int m_check(_type elem);
    ~Set(void);
private:
    _type * m_pelements;
    int m_setSize;
};

Set.cpp

    #pragma warning( disable : 4996 )
#include "Set.h" 
#include <iostream>
#include <string>



template <class _type>
Set<_type>::Set()
{
    m_setSize = 0;
}

template <class _type>
Set<_type>::Set(Set<_type> &_coll)
{
        m_setSize = _coll.m_setSize;
        m_pelements = new _type[_coll.m_setSize];
        for (int i = 0;i<m_setSize;i++)
        {
            m_pelements[i] = _coll.m_pelements[i];
        }

}

template <class _type>
Set<_type>::~Set()
{
    delete [] m_pelements;
}

template <class _type>
Set<_type> Set<_type>::m_add(Set<_type> _set1, Set<_type> _set2)
{
     Set<_type> finalSet;
    finalSet = _set1;
    for (int i = 0;i<_set2->m_setSize;i++)
    {
        m_addElem(finalSet, _set2->m_pelements[i]);
    }
    return finalSet;
}

template <class _type>
void Set<_type>::m_addElem(Set<_type> *const _set, _type _elem)
{

 if (_set->m_setSize == 0)
 {
_set->m_pelements = new _type[1];
    _set->m_pelements[0] = _elem;
    _set->m_setSize += 1;
}
else 
{
    _set->m_setSize += 1;
    _type * helpElements = new _type[_set->m_setSize];
    std::copy(_set->m_pelements, _set->m_pelements + _set->m_setSize-1,  helpElements);
    helpElements[_set->m_setSize-1] = _elem;
    delete [] _set->m_pelements;
    _set->m_pelements = helpElements;
    /*
    _type * helpElements = new _type[_set->m_setSize];
    for (int i = 0;i<_set->m_setSize;i++)
    {
        helpElements[i] =  _set->m_pelements[i];
    }
    delete _set->m_pelements;
    _set->m_setSize += 1;
    _set->m_pelements = new _type[_set->m_setSize];
    for (int i = 0;i<_set->m_setSize;i++)
    {
        _set->m_pelements[i] =  helpElements[i];
    }
    _set->m_pelements[_set->m_setSize-1] = _elem;
    */
}

}

template <class _type>
void Set<_type>::m_deleteElem(Set<_type> *const _set, _type _elem)
{
    int index = _set->m_check(_elem);
    if (index >= 0)
    {
        int k = 0;
        _set->m_setSize -= 1;
        _type * temp = new _type[_set->m_setSize];
        for (int i = 0;i<_set->m_setSize;i++)
      {
         if (i == index)
            k++;
        temp[i] = _set->m_pelements[i+k];
    }
    delete [] _set->m_pelements;
    _set->m_pelements = temp;
    }
}

template <class _type>
void Set<_type>::m_addArray(_type _elem[], int size)
{
    for (int i = 0;i<size;i++)
    {
    m_addElem(this,_elem[i]);
 }
}

template <class _type>
void Set<_type>::operator+( _type _elem)
{
    m_addElem(this,_elem);  
}

template <class _type>
Set<_type> Set<_type>::operator+(Set<_type> *const _set)
{
    return m_add(this,_set);    
}

template <class _type>
void Set<_type>::operator+( _type _elem[])
{
    m_addArray(this,_elem); 
}

template <class _type>
void Set<_type>::operator-( _type _elem)
{
    m_deleteElem(this,_elem);   
}

template <class _type>
Set<_type> & Set<_type>::operator=(Set<_type> &_set)
{
    if(&_set==this) return *this;

    delete [] m_pelements;

    m_setSize = _coll.m_setSize;
    m_pelements = new _type[_coll.m_setSize];
    for (int i = 0;i<m_setSize;i++)
    {
        m_pelements[i] = _coll.m_pelements[i];
    }

}

template <class _type>
void Set<_type>::m_display()
{
        for (int i = 0;i<m_setSize;i++)
        {
        std::cout << m_pelements[i] << "   " ;
        }
    std::cout << std::endl;
}

template <class _type>
int Set<_type>::m_check(_type _elem)
{
    for (int i = 0;i<m_setSize;i++)
    {
    if (m_pelements[i] == _elem)
         return i;
  }
return -1;
}

Main.cpp

#pragma warning( disable : 4996 )
#include "Set.h"
#include "Set.cpp"
#include <iostream>


int main()
{
    Set<std::string> zbior1;
    zbior1 + std::string("abc");
    zbior1 + std::string("abcd");
    zbior1 + std::string("abcdef");
    zbior1 + std::string("XD");
    zbior1.m_display();
    zbior1 - "XD";
    zbior1.m_display();

    std::string tablica[3] = {"ala", "ma", "kota" };
    zbior1.m_addArray(tablica,3);
    zbior1.m_display();

    Set<std::string> zbior2;
    zbior2 + std::string("abDDc");
    zbior2 + std::string("abcdDD");
    zbior2 + std::string("abcdeDDf");
    zbior2 + std::string("XDDD");
    zbior2.m_display();

    Set<std::string> zbior3;
    zbior3 = zbior1 + zbior2; //HERE'S THE PROBLEM
}

Problem appears int the last line of Main.cpp When arguments of Set operator+ are (Set *const _set) I get error " no operator found which takes a right-hand operand of type 'Set<_type>' (or there is no acceptable conversion)'" and if i remove *const there's different error saying "cannot convert parameter 1 from 'Set<_type> *const ' to 'Set<_type>'"

I have no idea how to repair it.

v1t0
  • 23
  • 7

2 Answers2

0

Your

Set operator+(Set *const _set);

is defined as taking a const pointer (which is for sure what you don't want), but you then pass to it an object instead, and not the address of an object. Pass by reference, like

Set operator+(Set const & _set);

Try reading Operator overloading for a very good introduction to the subject.

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
0

This method:

Set operator+(Set *const _set);

should probably look like:

Set operator+(Set const &rhs) const;

if it's going to stay a method. See the link in vsoftco's answer for why it should probably be a free non-friend function instead, implemented in terms of operator+=.

You pasted the same message for both errors, afaics, but at least one is complaining about the attempt to pass zbior2 to a method expecting a pointer.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • The second one is odd, but it should work correctly with the syntax in my asnswer. The canonical forms of operator overloads are well-documented, not least in that link. – Useless Jan 02 '16 at 18:52
  • It is not working. Right now I'm reading vsoftco's link and I will try to find a soultion. – v1t0 Jan 02 '16 at 18:55
  • The definition of `operator+` in your .cpp file doesn't match the declaration at all, so this code must be wrong anyway. You might just have to try again with a minimal complete example. – Useless Jan 03 '16 at 12:59