我不确定为什么会收到此错误:“ C ++没有运算符匹配这些操作数,操作数类型为:Set <int> = Set <int>”

问题描述

当我尝试将'A && B'分配给outSet时,它告诉我没有运算符'='匹配,并且我不确定为什么我的赋值运算符不起作用。我的目标最终是证明A B和C之间的以下比较,但不幸的是,我坚持认为自己可以做到这一点。任何帮助将不胜感激

我的.cpp文件

#include "set.h"
#include <algorithm>
#include <iostream>



int main()
{
    Set<int> A(1,8);
    Set<int> B(2,10);
    Set<int> C(4,6);
    Set<int> outSet(0,20);

    A.add(1);A.add(3);A.add(8);
    B.add(2);B.add(3);B.add(5);B.add(10);
    C.add(4);C.add(6);

    A.writeSet();
    B.writeSet();
    C.writeSet();
    
    
    
    outSet = A&&B; //test assignment and intersection operator
    outSet.writeSet();


    /*
    outSet = A-B; //test difference
    outSet.writeSet();
    outSet = A||B; //test union
    outSet.writeSet();
    outSet = A/B; 
    outSet.writeSet();

    outSet = A&&C;
    outSet.writeSet();
    outSet = A-C;
    outSet.writeSet();
    outSet = A||C;
    outSet.writeSet();
    outSet = A/C;
    outSet.writeSet();
    */


    return 0;
}

我的set.h文件

#ifndef SET_H
#define SET_H


#include "array_v.h"
#include <iostream>
#include <cassert>


template <class Universe>
class Set : protected Array_V<Universe,bool>
{
public:
Set(Universe loElement,Universe hiElement);
Set(Set <Universe>& initSet);
~Set();
void operator = (Set<Universe>& source);
bool empty();
bool operator == (Set<Universe>& t);
bool operator <= (Set<Universe>& t);
Set operator || (Set<Universe>& t);//union
Set operator && (Set<Universe>& t);//intersection
Set operator - (Set<Universe>& t); //a-b= elements in a not in b
Set operator / (Set<Universe>& t); //a/b= elements in union ab minus
                    //elements in intersection ab
void add(Universe element);
void remove(Universe element);
void writeSet();
bool inSet(Universe element);
protected:
Universe loElement,hiElement;
};




#include "set.t"



#endif

我的set.t文件

#ifndef SET_T_
#define SET_T_


#include <iostream>
using std::cout;
using std::endl;
using std::cerr;

#include <new>
using std::bad_alloc;

#include <cassert> 


template <class Universe>
Set<Universe>::Set(Universe lo,Universe hi):
    Array_V<Universe,bool>(lo,hi)
{
  loElement = lo;
  hiElement = hi;
  for (Universe element = loElement; element <=hiElement; ++element)
    (*this)[element] = false;
}
template <class Universe>
Set <Universe>:: Set(Set<Universe> &initSet)
    :Array_V<Universe,bool> (initSet.loElement,initSet.hiElement)
{
  loElement = initSet.loElement;
  hiElement = initSet.hiElement;
  for (Universe element = loElement; element <=hiElement; ++element)
    (*this)[element] = initSet[element];
}
template<class Universe>
Set <Universe> Set<Universe>::operator || ( Set<Universe>&t)
{
  Set<Universe> temp(loElement,hiElement);
  if ((loElement!=t.loElement)||(hiElement!=t.hiElement))
    {
      cout << " && invalid ranges" << endl;
      return (*this);
    }
  else for (Universe u = loElement; u <= hiElement; ++u)
      temp[u] = ((*this)[u] || t[u]);
  return temp;
}
template<class Universe>
Set <Universe> Set<Universe>::operator && ( Set<Universe>&t )
{
  Set<Universe> temp(loElement,hiElement);
  if ((loElement!=t.loElement)||(hiElement!=t.hiElement))
    {
      cout << " && invalid ranges" << endl;
      return (*this);
    }
  else for (Universe u = loElement; u <= hiElement; ++u)
      temp[u] = ((*this)[u] && t[u]);
  return temp;
}
template <class Universe>
Set<Universe>::~Set()
{}
template <class Universe>
void Set<Universe>:: operator = ( Set<Universe> &source )
{
  if ((loElement != source.loElement) || (hiElement != source.hiElement))
    cout << " invalid assignment: incompatable ranges " << endl;
  else
    for (Universe el = loElement; el <= hiElement; el ++)
      (*this)[el] = source[el];
}
template <class Universe>
bool Set<Universe>::empty()
{
  bool temp = true;
  for (Universe el = loElement; el <= hiElement; el++)
    if ((*this)[el]) temp = false;
  return temp;
}
template <class Universe>
bool Set<Universe>::operator == ( Set<Universe> &t )
{
  bool temp = true;
  if ((loElement!=t.loElement)||(hiElement!=t.hiElement))
    {
      cout << " == invalid ranges" << endl;
      return false;
    }
  else
    {
      for (Universe el = loElement; el <=hiElement; el++)
        if ((*this)[el]!=t[el])
          temp = false;
      return temp;
    }
}
template <class Universe>
bool Set<Universe>::operator <= ( Set<Universe>&t )
{
  bool temp = true;
  if ((loElement!=t.loElement)||(hiElement!=t.hiElement))
    {
      cout << " <= invalid ranges" << endl;
      return false;
    }
  else
    {
      for (Universe el = loElement; el <=hiElement; el++)
        if ((*this)[el]&&(!t[el]))
          temp = false;
      return temp;
    }
}
template <class Universe>
void Set<Universe>::add(Universe el )
  {
    (*this).assign(el,true);
  }
template <class Universe>
void Set<Universe>::remove(Universe el )
  {
    (*this)[el] = false;
  }
template <class Universe>
void Set<Universe>:: writeSet()
{
  bool comma = false;
  cout << '(';
  for (Universe el = loElement; el <=hiElement; el++ )
    {
      if (comma && (*this)[el]) cout << ',';
      if ((*this)[el])
        {
          cout << el ;
          comma = true;
        }
    }
  cout << ')' << endl;
}
#endif

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)