无法理解此错误... "Type 'const intSet' does not provide a subscript operator"这是与所有星号一致的

Can't understand this error... "Type 'const intSet' does not provide a subscript operator" this is at the line with all of the asterisks

本文关键字:operator subscript not 错误 Type const provide does intSet      更新时间:2023-10-16

这是我的.cpp文件

// FILE: IntSet.cpp - header file for IntSet class
//       Implementation file for the IntStore class
//       (See IntSet.h for documentation.)
// INVARIANT for the IntSet class:
// (1) Distinct int values of the IntSet are stored in a 1-D,
//     compile-time array whose size is IntSet::MAX_SIZE;
//     the member variable data references the array.
// (2) The distinct int value with earliest membership is stored
//     in data[0], the distinct int value with the 2nd-earliest
//     membership is stored in data[1], and so on.
//     Note: No "prior membership" information is tracked; i.e.,
//           if an int value that was previously a member (but its
//           earlier membership ended due to removal) becomes a
//           member again, the timing of its membership (relative
//           to other existing members) is the same as if that int
//           value was never a member before.
//     Note: Re-introduction of an int value that is already an
//           existing member (such as through the add operation)
//           has no effect on the "membership timing" of that int
//           value.
// (4) The # of distinct int values the IntSet currently contains
//     is stored in the member variable used.
// (5) Except when the IntSet is empty (used == 0), ALL elements
//     of data from data[0] until data[used - 1] contain relevant
//     distinct int values; i.e., all relevant distinct int values
//     appear together (no "holes" among them) starting from the
//     beginning of the data array.
// (6) We DON'T care what is stored in any of the array elements
//     from data[used] through data[IntSet::MAX_SIZE - 1].
//     Note: This applies also when the IntSet is empry (used == 0)
//           in which case we DON'T care what is stored in any of
//           the data array elements.
//     Note: A distinct int value in the IntSet can be any of the
//           values an int can represent (from the most negative
//           through 0 to the most positive), so there is no
//           particular int value that can be used to indicate an
//           irrelevant value. But there's no need for such an
//           "indicator value" since all relevant distinct int
//           values appear together starting from the beginning of
//           the data array and used (if properly initialized and
//           maintained) should tell which elements of the data
//           array are actually relevant.
#include "IntSet.h"
#include <iostream>
#include <cassert>
using namespace std;

IntSet::IntSet()
{
    //initializing both of the private elements
    int data[MAX_SIZE];//max size to be changed
    int used(0);//I don't know what this stands for...
}
int IntSet::size() const//complete
{
    int m=0;
    while(data[m]!='')
    {
        m++;
    }
    return m;
}
bool IntSet::isEmpty() const//complete
{
    int m(0);
    while(data[m]!='')
    {
        m++;
    }
    if(m==0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
bool IntSet::contains(int anInt) const//complete
{
    int m(0);//iterator
    while(data[m]!=anInt)
    {
        m++;
    }
    if(m!=0)//if it does contain this number
    {
        return 1;
    }
    else //if it doesn't contain this number
    {
        return 0;
    }
}
bool IntSet::isSubsetOf(const IntSet& otherIntSet) const
{
    //variable initialization
    int k=0;
    int e=0;
    int m=0;
    int f=0;
    while(m != otherIntSet.size())
    {
        while(e!=1)
        {
            while(k != MAX_SIZE)
            {
                cout<<k<<endl;//debug
                if(IntSet[k] = otherIntSet[m])//********************
                {
                    f++;//found one more
                    e = 1;//exit = 1
                }
                else
                {
                    k++;
                }
            }
        }
        if(f == otherIntSet.size())
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
   return false; // dummy value returned
}
void IntSet::DumpData(ostream& out) const
{  // already implemented ... DON'T change anything
   if (used > 0)
   {
      out << data[0];
      for (int i = 1; i < used; ++i)
         out << "  " << data[i];
   }
}
IntSet IntSet::unionWith(const IntSet& otherIntSet) const
{
   cout << "unionWith() is not implemented yet..." << endl;
   return IntSet(); // dummy IntSet object returned
}
IntSet IntSet::intersect(const IntSet& otherIntSet) const
{
   cout << "intersect() is not implemented yet..." << endl;
   return IntSet(); // dummy IntSet object returned
}
IntSet IntSet::subtract(const IntSet& otherIntSet) const
{
   cout << "subtract() is not implemented yet..." << endl;
   return IntSet(); // dummy IntSet object returned
}
void IntSet::reset()
{
   cout << "reset() is not implemented yet..." << endl;
}
bool IntSet::add(int anInt)
{
   cout << "add() is not implemented yet..." << endl;
   return false; // dummy value returned
}
bool IntSet::remove(int anInt)
{
   cout << "remove() is not implemented yet..." << endl;
   return false; // dummy value returned
}
bool equal(const IntSet& is1, const IntSet& is2)
{
   cout << "equal() is not implemented yet..." << endl;
   return false; // dummy value returned
}

头文件

// FILE: IntSet.h - header file for IntSet class
// CLASS PROVIDED: IntSet (a container class for a set of
//                 int values)
//
// CONSTANT
//   static const int MAX_SIZE = ____
//     IntSet::MAX_SIZE is the highest # of elements an IntSet
//     can accommodate.
//
// CONSTRUCTOR
//   IntSet()
//     Pre:  (none)
//     Post: The invoking IntSet is initialized to an empty
//           IntSet (i.e., one containing no relevant elements).
//
// CONSTANT MEMBER FUNCTIONS (ACCESSORS)
//   int size() const
//     Pre:  (none)
//     Post: Number of elements in the invoking IntSet is returned.
//   bool isEmpty() const
//     Pre:  (none)
//     Post: True is returned if the invoking IntSet has no relevant
//           relevant elements, otherwise false is returned.
//   bool contains(int anInt) const
//     Pre:  (none)
//     Post: true is returned if the invoking IntSet has anInt as an
//           element, otherwise false is returned.
//   bool isSubsetOf(const IntSet& otherIntSet) const
//     Pre:  (none)
//     Post: True is returned if all elements of the invoking IntSet
//           are also elements of otherIntSet, otherwise false is
//           returned.
//           By definition, true is returned if the invoking IntSet
//           is empty (i.e., an empty IntSet is always isSubsetOf
//           another IntSet, even if the other IntSet is also empty).
//   void DumpData(std::ostream& out) const
//     Pre:  (none)
//     Post: Contents of the invoking IntSet have been inserted into
//           out with 2 spaces separating one item from another if
//           if there are 2 or more items.
//   IntSet unionWith(const IntSet& otherIntSet) const
//     Pre:  size() + (otherIntSet.subtract(*this)).size() <= MAX_SIZE
//     Post: An IntSet representing the union of the invoking IntSet
//           and otherIntSet is returned.
//     Note: Equivalently (see postcondition of add), the IntSet
//           returned is one that initially is an exact copy of the
//           invoking IntSet but subsequently has all elements of
//           otherIntSet added.
//   IntSet intersect(const IntSet& otherIntSet) const
//     Pre:  (none)
//     Post: An IntSet representing the intersection of the invoking
//           IntSet and otherIntSet is returned.
//     Note: Equivalently (see postcondition of remove), the IntSet
//           returned is one that initially is an exact copy of the
//           invoking IntSet but subsequently has all of its elements
//           that are not also elements of otherIntSet removed.
//   IntSet subtract(const IntSet& otherIntSet) const
//     Pre:  (none)
//     Post: An IntSet representing the difference between the invoking
//           IntSet and otherIntSet is returned.
//     Note: Equivalently (see postcondition of remove), the IntSet
//           returned is one that initially is an exact copy of the
//           invoking IntSet but subsequently has all elements of
//           otherIntSet removed.
//
// MODIFICATION MEMBER FUNCTIONS (MUTATORS)
//   void reset()
//     Pre:  (none)
//     Post: The invoking IntSet is reset to become an empty IntSet
//           (i.e., one containing no relevant elements).
//   bool add(int anInt)
//     Pre:  contains(anInt) ? size() <= MAX_SIZE : size() < MAX_SIZE
//     Post: If contains(anInt) returns false, anInt has been
//           added to the invoking IntSet as a new element and
//           true is returned, otherwise the invoking IntSet is
//           unchanged and false is returned.
//   bool remove(int anInt)
//     Pre:  (none)
//     Post: If contains(anInt) returns true, anInt has been
//           removed from the invoking IntSet and true is
//           returned, otherwise the invoking IntSet is unchanged
//           and false is returned.
//
// NON-MEMBER FUNCTIONS
//   bool equal(const IntSet& is1, const IntSet& is2)
//     Pre:  (none)
//     Post: True is returned if is1 and is2 have the same elements,
//           otherwise false is returned; for e.g.: {1,2,3}, {1,3,2},
//           {2,1,3}, {2,3,1}, {3,1,2}, and {3,2,1} are all equal.
//     Note: By definition, two empty IntSet's are equal.
#ifndef INT_SET_H
#define INT_SET_H
#include <iostream>
class IntSet
{
public:
   static const int MAX_SIZE = 10;
   IntSet();
   int size() const;
   bool isEmpty() const;
   bool contains(int anInt) const;
   bool isSubsetOf(const IntSet& otherIntSet) const;
   void DumpData(std::ostream& out) const;
   IntSet unionWith(const IntSet& otherIntSet) const;
   IntSet intersect(const IntSet& otherIntSet) const;
   IntSet subtract(const IntSet& otherIntSet) const;
   void reset();
   bool add(int anInt);
   bool remove(int anInt);
private:
   int data[MAX_SIZE];
   int used;
};
bool equal(const IntSet& is1, const IntSet& is2);
#endif

我不知道我在这里需要做什么。。。我也不知道如何为Unionwith和intersect函数编写代码。。。从我的代码中的这一点到最后的任何帮助都将不胜感激。我在数据结构领域,它正在变得真实!非常感谢。

下面的行不对。

if(IntSet[k] = otherIntSet[m])
  1. CCD_ 1是一种类型。使用IntSet[k]根本没有意义。

  2. IntSetoperator[]函数没有过载。因此,otherIntSet[m]是不对的。

  3. 您可能想要使用==,而不是=

我猜你打算使用:

if ( this->data[k] == otherIntSet.data[m] )

如果operator[]功能过载为:

int& operator[](size_t index)
{
   return data[m];
}
int operator[](size_t index) const
{
   return data[m];
}

您可以使用:

if( (*this)[k] == otherIntSet[m] )