Solved

compiling problems

Posted on 1998-07-12
3
217 Views
Last Modified: 2010-04-01
I want to use templates in this program.  It is the same one i had asked in my previous question. If you need to see the original source code, please refer to the previous question. Thanks

here is my source code..




#include <iostream.h>
#include <string.h>

#define MAX 101

template<class T>
class GenericSet
{
   private:
    T a[MAX];
      int count;
 
   public:
    GenericSet();
   
   void unionOfGenericSets(GenericSet a1, GenericSet a2);
 //   void intersectionOfGenericSets(GenericSet a1, GenericSet a2);
    void insertElement(T elt);
    void deleteElement(T elt);
    void setPrint();
 //   bool isEqualTo(GenericSet a1);


 
};


template<class T>
GenericSet<T>::GenericSet()
{
      count=0;      
}



template<class T>
void GenericSet<T>::unionOfGenericSets(GenericSet a1, GenericSet a2)
{
      bool exists;
      int i,j ;

    count=0;
    for(i=0; i<a1.count; i++)
            insertElement(a1[i]);



    for(i=0; i<a2.count; i++)
    {
         exists=false;
         for(j=0; j<count; j++)
         {
               if(a2[i]==a[j])
                     exists = true;
         }
         if(exists==false)
               insertElement(a2[i]);
    }
}


template <class T>
void GenericSet<T>::insertElement(T elt)
{
      if (count < MAX)
      {
            a[count]=elt;
            count ++;
      }
}


template <class T>
void GenericSet<T>::deleteElement(T elt)
{
      int i;
      GenericSet<T> temp_a;

      for(i=0; i<count; i++)
            if(a[i]!=elt)      
                  temp_a.insertElement(a[i]);
            
      count=0;
      for(i=0; i<temp_a.count; i++)
      {
            insertElement(temp_a[i]);
      }
            
}

      /*
    void GenericSet::intersectionOfGenericSets(GenericSet a1, GenericSet a2)
{
    int i=0;
    while(i < 101)
 {
    if(a1.SetElement(i)==1 && a2.SetElement(i)==1)
  {
    a[i]=1;
  }
    i=i+1;
 }
}
*/

template<class T>
void GenericSet<T>::setPrint()
{
      int i;

      if(count)
      {
            for(i=0; i<count; i++)
                  cout << a[i] << "  ";
      }
      else
            cout << "---";

    cout << "\n";
}

/*
    bool GenericSet::isEqualTo(GenericSet a1)
{
    int i=0;
    while(i < 101)
 {
    if(a[i] != a1.SetElement(i))
  {
    return false;
  }
    i=i+1;
 }
    return true;
}
*/



/*----------------------- DRIVER --------------------------------------*/
void main()
{
      GenericSet<int> a, a1, a2;


      a1.insertElement(1);
      a1.insertElement(2);
      a1.insertElement(3);

      a2.insertElement(2);
      a2.insertElement(3);
      a2.insertElement(4);

      a.unionOfGenericSets(a1, a2);


//      a.deleteElement(10);

      a.setPrint();

            /*
    GenericSet a(80, 50, 100, 12, 15), b(67, 50, 15, 80, 43), c, un, intrsctn;

    cout << "a: ";
    a.setPrint();
    cout << "b: ";
    b.setPrint();

    if(a.isEqualTo(b))
 {
    cout << "These sets are equal\n";
 }
    else
 {
    cout << "These sets are unequal\n";
 }

    cout << "Their union:\n";
    un.unionOfGenericSets(a, b);
    un.setPrint();

    cout << "Their intersection:\n";
    intrsctn.intersectionOfGenericSets(a, b);
    intrsctn.setPrint();

    cout << "c: ";
    c.setPrint();

    a.deleteElement(12);
    a.deleteElement(100);
    cout << "Delete 12 and 100 from a1. Now\na:";
    a.setPrint();

    a.insertElement(67);
    a.insertElement(43);
    cout << "Insert 43 and 67 into a1. Now\na:";
    a.setPrint();

    if(a.isEqualTo(b))
 {
    cout << "a equals b\n";
 }
 */
}

when you compile this program, you will see the errors and please give me whatever solution you have.
thanks.
0
Comment
Question by:ash2902
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 20 total points
ID: 1167600
template<class T>
void GenericSet<T>::unionOfGenericSets(GenericSet a1, GenericSet a2)
{
    bool exists;
    int i,j ;
   
    count=0;
    for(i=0; i<a1.count; i++)
        insertElement(a1.a[i]);
   
    for(i=0; i<a2.count; i++)
    {
        exists=false;
        for(j=0; j<count; j++)
        {
            if(a2.a[i]==a[j])
                exists = true;
        }
        if(exists==false)
            insertElement(a2.a[i]);
    }
}

0
 

Author Comment

by:ash2902
ID: 1167601
Thanks a lot Alexo.....do you have comments for the intersection of sets? and also the delete function?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167602
Actually, I just looked at the compilation errors...

<squinting at the code> ...

Using a C array to represent sets is inefficient.  Use binary trees if you can.

However, given your design choice:

deleteItem does not delete anything.  You can traverse the array until you find a matching item, then in a loop do something like a[i] = a[i+1] and decrement count.

The union and intersection should take just one argument (the other one is the implicit "this" set) and return a newly constructed set by value.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question