簡體   English   中英

我不確定為什么我會收到這個錯誤:“C++ 沒有運算符匹配這些操作數操作數類型是:設置<int> = 設置<int> ”

[英]I'm unsure why I am getting this error: "C++ no operator matches these operands operand types are: Set<int> = Set<int>"

當我嘗試將 'A&&B' 分配給 outSet 時,它告訴我沒有運算符 '=' 匹配,我不確定為什么我的賦值運算符不起作用。 我的最終目標是展示 AB 和 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
A&&B

這個表達式的結果是一個臨時值,一個純右值。

void operator = (Set<Universe>& source);

賦值運算符重載的參數是對可變對象的引用。

臨時值不綁定到對可變對象的引用; 但僅限於對常量對象的引用。 您需要將重載更改為:

void operator = (const Set<Universe>& source);

並相應地更改其代碼。

所有其他運算符重載也應該以相同的方式更改。

此外,按照慣例, =重載應該返回*this ,所以它應該聲明為

Set<Universe> &operator = (const Set<Universe>& source);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM