|
From: bhadram r. <bha...@ya...> - 2005-09-05 10:05:22
|
Dear friends,
I'm facing with a problem with templates.I'm including all my code at end.The sole purpose of my program is that to use heap algorithm which will take template parameter like this
MaxHeap<Array<String> > m(10);
m.initilize(a,10,10);
where String is self created class which will work same as string class and Array class main function is to use indexing starting with 1 rather than zero. The problem is that in the following line
if((c<currentSize) && (heap[c]<heap[c+1]))
c++;
As my knoweledge goes in accordance with operator precedence heap[c] is worked out([] operator is over loaded in Array class) and as you see in the code it returns String object, in according to the following code in Array class.As Array class is instatiated as Array<String>, then template must do the same.The same will happen with heap[c+1] and finally < must operate on two String classes objects and < overload operator must operate and returns the int value which is furhter evaluated. But I'm getting compilation errors like this.
T& operator[](int i)
{
return data[i-1];
}
I couldnt understand why Array<class bhadram1:String> is transferring while comparision because in precedence it is low.I couldnt understand what is happening and i want to clear about templates. Please help me in this aspect.
compilation errors:
:\cpppractice\sep3\heap1.h(72) : error C2039: 'getPointer' : is not a member of 'Array<class bhadram1::String>'
d:\cpppractice\sep3\heap1.h(115) : see reference to function template instantiation 'int __cdecl bhadram1::operator <(class bhadram1::Array<class bhadram1::String> &,class bhadram1::Array<class bhadram1::String> &)' being compiled
d:\cpppractice\sep3\heap1.h(72) : error C2039: 'getPointer' : is not a member of 'Array<class bhadram1::String>'
d:\cpppractice\sep3\heap1.h(115) : see reference to function template instantiation 'int __cdecl bhadram1::operator <(class bhadram1::Array<class bhadram1::String> &,class bhadram1::Array<class bhadram1::String> &)' being compiled
d:\cpppractice\sep3\heap1.h(65) : error C2039: 'getPointer' : is not a member of 'Array<class bhadram1::String>'
d:\cpppractice\sep3\heap1.h(117) : see reference to function template instantiation 'int __cdecl bhadram1::operator >=(class bhadram1::Array<class bhadram1::String> &,class bhadram1::Array<class bhadram1::String> &)' being compiled
d:\cpppractice\sep3\heap1.h(65) : error C2039: 'getPointer' : is not a member of 'Array<class bhadram1::String>'
d:\cpppractice\sep3\heap1.h(117) : see reference to function template instantiation 'int __cdecl bhadram1::operator >=(class bhadram1::Array<class bhadram1::String> &,class bhadram1::Array<class bhadram1::String> &)' being compiled
Error executing cl.exe.
heap.obj - 4 error(s), 0 warning(s)
# include <iostream>
# include <Heap1.h>
# include <String1.h>
using namespace std;
using namespace bhadram1;
int main()
{
int array[10]={20,12,35,15,10,80,30,17,2,1};
String str[10]={"first","second","third","fourth","fifth","sixth","seventh","eight","nine","ten"};
Array<String> a(str,10);
MaxHeap<Array<String> > m(10);
m.initilize(a,10,10);
//m.print();
return 0;
}
Heap1.h file:
# include <iostream>
using namespace std;
namespace bhadram1
{
template<class T>
class Array
{
//friend ostream& operator<<(ostream& ostr,String& s);
protected:
int size;
T* data;
public:
Array()
{
size=1;
data=new T[1];
}
Array(int n)
{
size=n;
data=new T[size];
}
Array(T* a,int const s)
{
size=s;
data=new T[size];
for(int i=0;i<s;i++)
{
data[i]=a[i];
//cout<<data[i]<<endl;
}
}
Array(Array<T>& a)
{
size=a.size;
//delete[] data;
data=new T[size];
for(int i=0;i<size;i++)
{
data[i]=a.data[i];
}
}
T& operator[](int i)
{
return data[i-1];
}
T& operator=(T& a)
{
return a;
}
friend ostream& operator<<(ostream& ostr,T& a);
friend int operator<(T& a1,T& a2);
friend int operator>=(T& a1,T& a2);
};
template<class T>
int operator>=(T& a1,T& a2)
{
return (strcmp(a1.getPointer(),a2.getPointer()>=0));
}
template<class T>
int operator<(T& a1,T& a2)
{
return (strcmp(a1.getPointer(),a2.getPointer()<0));
}
template<class T>
ostream& operator<<(ostream& ostr,T& a)
{
return ostr<<a;
}
template<class T>
class MaxHeap
{
private:
int currentSize,maxSize;
T* heap;
public:
MaxHeap(int n)
{
currentSize=n;
heap=new T[n];
}
void initilize(T a,int size,int arraySize)
{
for(int j=1;j<=10;j++)
{
heap[j]=a[j];
cout<<heap[j];
}
currentSize=size;
maxSize=arraySize;
for(int i=currentSize/2;i>=1;i--)
{
T y=heap[i];
//cout<<"a "<<a[i]<<" y "<<y;
int c=2*(i);
//cout<<"c "<<c<<" current "<<currentSize<<endl;
while(c<=currentSize)
{
//cout<<"sub c "<<c<<endl;
if((c<currentSize) && (heap[c]<heap[c+1]))
c++;
if(y>=heap[c])
break;
heap[c/2]=heap[c];
c*=2;
}
heap[c/2]=y;
}
for(i=1;i<=10;i++)
{
cout<<heap[i]<<endl;
}
}
void print()
{
}
};
};
String1.h file:
using namespace std;
namespace bhadram1
{
class String
{
private:
char* _string;
int _size;
public:
String(int n=0);
String(String& s);
String(const char* );
String& operator=(String& s);
String& operator=(char* ch);
friend ostream& operator<<(ostream& ostr,String& s);
String operator+(String&);
String operator+(char* );
String& operator+=(String& s);
friend String operator+(const char* c,String& s);
//char& operator[](int n);
int operator<(String& s);
int operator>(String& s);
String& operator[](int n);
char* getPointer();
};
char* String::getPointer()
{
return _string;
}
String& String::operator =(char* ch)
{
int _size=strlen(ch);
_string=new char[_size+1];
for(int i=0;i<_size;i++)
{
_string[i]=*ch++;
}
_string[i]='\0';
return *this;
}
String& String::operator[](int n)
{
return *this;
}
int String::operator<(String& s)
{
return (strcmp(_string,s._string)<0);
}
int String::operator >(String& s)
{
return (strcmp(s._string,_string)>0);
}
/*char& String::operator [](int n)
{
return _string[n];
}*/
String operator+(const char* c,String& s)
{
String temp;
temp._size=strlen(c)+s._size;
strcpy(temp._string,c);
strcat(temp._string,s._string);
return temp;
}
String String::operator +(char* c)
{
String temp;
temp._size=_size+strlen(c);
strcpy(temp._string,_string);
strcat(temp._string,c);
return temp;
}
String& String::operator +=(String& s)
{
String temp;
temp._size=_size+s._size;
strcpy(temp._string,_string);
strcat(temp._string,s._string);
delete [] _string;
_size=temp._size;
_string=new char[temp._size+1];
strcpy(_string,temp._string);
return *this;
}
String String::operator+(String& s1)
{
String temp;
temp._size=_size+s1._size;
strcpy(temp._string,_string);
strcat(temp._string,s1._string);
return temp;
}
String& String::operator=(String& s)
{
_size=s._size;
delete [] _string;
_string=new char[_size+1];
for(int i=0;i<_size;i++)
_string[i]=s._string[i];
_string[i]='\0';
return *this;
}
String::String(int n)
{
_size=n;
_string=new char[_size+1];
for(int i=0;i<_size;i++)
_string[i]=' ';
_string[i]='\0';
}
String::String(String& s)
{
_size=s._size;
_string=new char[_size+1];
for(int i=0;i<_size;i++)
_string[i]=s._string[i];
_string[i]='\0';
}
String::String(const char* ch)
{
_size=strlen(ch);
_string=new char[_size+1];
for(int i=0;i<_size;i++)
_string[i]=*ch++;
_string[i]='\0';
}
ostream& operator<<(ostream& ostr,String& s)
{
return ostr<<s._string;
}
};
Thanking you,
yours friendly,
bhadram
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com |