Solved

compiling problems

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cell Value from TStringGrid::StringGrid1DrawColumnCell function in C++ builder XE8 3 106
Dialogbox API leak? 18 118
Unable to start eclipse ? 17 174
print bytes of an integer 6 48
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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