没有匹配的操作数[template] /无法从Type const *转换为Type

时间:2016-01-02 18:31:12

标签: c++ templates

让我先发布我的代码:

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
}

问题出现在Main.cpp的最后一行 当Set operator+的参数为(Set *const _set)时,我收到错误“找不到运算符,该运算符采用'Set&lt; _type&gt;'类型的右手操作数(或者没有可接受的转换)'“如果我删除*const,则会出现”不能将参数1从'Set&lt; _type&gt; * const'转换为'Set&lt; _type&gt;'

的错误

我不知道如何修复它。

2 个答案:

答案 0 :(得分:0)

Set operator+(Set *const _set);

被定义为使用const指针(这肯定是你不想要的),但是然后你传递给它一个对象,而不是对象的地址。通过引用传递,如

Set operator+(Set const & _set);

尝试阅读Operator overloading以获得对该主题的非常好的介绍。

答案 1 :(得分:0)

此方法:

Set operator+(Set *const _set);

应该看起来像:

Set operator+(Set const &rhs) const;

如果要保留一种方法。请参阅vsoftco答案中的链接,了解为什么它应该是一个免费的非朋友功能,而不是operator+=

您为这两个错误粘贴了相同的消息,afaics,但至少有一个抱怨尝试将zbior2传递给期望指针的方法。