Solved

compiling problems

Posted on 1998-07-12
3
203 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

943 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now