Click here to Skip to main content
16,012,223 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CHttpFile Pin
Neagoe Gabriel5-Sep-05 4:23
Neagoe Gabriel5-Sep-05 4:23 
AnswerRe: CHttp Pin
MailtoGops5-Sep-05 3:12
MailtoGops5-Sep-05 3:12 
AnswerRe: CHttp Pin
Gary R. Wheeler5-Sep-05 4:04
Gary R. Wheeler5-Sep-05 4:04 
Questionhwnd Pin
Ali Tavakol5-Sep-05 1:53
Ali Tavakol5-Sep-05 1:53 
AnswerRe: hwnd Pin
Bob Stanneveld5-Sep-05 2:00
Bob Stanneveld5-Sep-05 2:00 
Answer[Message Deleted] Pin
Neagoe Gabriel5-Sep-05 2:00
Neagoe Gabriel5-Sep-05 2:00 
AnswerRe: hwnd Pin
Neagoe Gabriel5-Sep-05 2:03
Neagoe Gabriel5-Sep-05 2:03 
Questionproblem with templates please help Pin
Member 22477495-Sep-05 0:03
Member 22477495-Sep-05 0:03 
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([]="" over="" loaded="" array="" class)="" and="" as="" you="" see="" the="" code="" it="" returns="" string="" object,="" according="" to="" following="" class.as="" class="" instatiated="" 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];
="" operator="(T&" a)
="" a;
="" friend="" ostream&="" operator<<(ostream&="" ostr,t&="" a);
="" int="" operator<(t&="" a1,t&="" a2);
="">=(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<<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="" );
="" string&="" operator="(String&" ch);
="" friend="" ostream&="" operator<<(ostream&="" ostr,string&="" string="" operator+(string&);
="" operator+(char*="" operator+="(String&" operator+(const="" c,string&="" char&="" operator[](int="" n);
="" operator<(string&="">(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<
QuestionHotFix Pin
Supriya Tonape4-Sep-05 23:57
Supriya Tonape4-Sep-05 23:57 
AnswerRe: HotFix Pin
ThatsAlok5-Sep-05 0:09
ThatsAlok5-Sep-05 0:09 
GeneralRe: HotFix Pin
Supriya Tonape5-Sep-05 1:46
Supriya Tonape5-Sep-05 1:46 
GeneralRe: HotFix Pin
ThatsAlok5-Sep-05 2:49
ThatsAlok5-Sep-05 2:49 
GeneralRe: HotFix Pin
Ravi Bhavnani5-Sep-05 3:05
professionalRavi Bhavnani5-Sep-05 3:05 
GeneralRe: HotFix Pin
Supriya Tonape5-Sep-05 3:21
Supriya Tonape5-Sep-05 3:21 
GeneralRe: HotFix Pin
ThatsAlok5-Sep-05 3:49
ThatsAlok5-Sep-05 3:49 
Questionsimple Question Pin
Eytukan4-Sep-05 23:54
Eytukan4-Sep-05 23:54 
AnswerRe: simple Question [edited] Pin
toxcct5-Sep-05 0:05
toxcct5-Sep-05 0:05 
AnswerRe: simple Question Pin
ThatsAlok5-Sep-05 0:06
ThatsAlok5-Sep-05 0:06 
JokeRe: simple Question Pin
G Haranadh5-Sep-05 0:32
G Haranadh5-Sep-05 0:32 
JokeRe: simple Question Pin
ThatsAlok5-Sep-05 0:35
ThatsAlok5-Sep-05 0:35 
JokeRe: simple Question Pin
toxcct5-Sep-05 1:07
toxcct5-Sep-05 1:07 
GeneralRe: simple Question Pin
kakan5-Sep-05 0:36
professionalkakan5-Sep-05 0:36 
AnswerRe: simple Question Pin
ThatsAlok5-Sep-05 1:12
ThatsAlok5-Sep-05 1:12 
GeneralRe: simple Question Pin
kakan5-Sep-05 1:35
professionalkakan5-Sep-05 1:35 
AnswerRe: simple Question Pin
ThatsAlok5-Sep-05 3:12
ThatsAlok5-Sep-05 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.