Solved

compiling problems

Posted on 1998-07-12
3
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

636 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