Solved

compiling problems

Posted on 1998-07-12
3
194 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

6 Experts available now in Live!

Get 1:1 Help Now