Guys I have this simple code on templates
#include <iostream.h>
#include <vector>
template <typename T>
class MyQueue
{
std::vector<T> data;
public:
void Add(T const &);
void Remove();
void Print();
};
template <typename T> void MyQueue<T> ::Add(T const &d)
{
data.push_back(d);
}
template <typename T> void MyQueue<T>::Remove()
{
data.erase(data.begin( ) + 0,data.begin( ) + 1);
}
template <typename T> void MyQueue<T>::Print()
{
std::vector <double>::iterator It1;
It1 = data.begin();
for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
cout << " " << *It1<<endl;
}
int main (int argc,char *argv[])
{
MyQueue<double> q;
q.Add(1.34);
q.Add(2.24);
cout<<"Before removing data"<<endl;
q.Print();
q.Remove();
cout<<"After removing data"<<endl;
q.Print();
return 0;
}
#include <iostream.h>
#include <vector>
template <typename T>
class MyQueue
{
std::vector<T> data;
public:
void Add(T const &);
void Remove();
void Print();
};
template <typename T> void MyQueue<T> ::Add(T const &d)
{
data.push_back(d);
}
template <typename T> void MyQueue<T>::Remove()
{
data.erase(data.begin( ) + 0,data.begin( ) + 1);
}
template <typename T> void MyQueue<T>::Print()
{
std::vector <double>::iterator It1;
It1 = data.begin();
for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
cout << " " << *It1<<endl;
}
int main (int argc,char *argv[])
{
MyQueue<double> q;
q.Add(1.34);
q.Add(2.24);
cout<<"Before removing data"<<endl;
q.Print();
q.Remove();
cout<<"After removing data"<<endl;
q.Print();
return 0;
}
To copy to clipboard, switch view to plain text mode
in my
template <typename T> void MyQueue<T>::Print()
method I want this iterator to be synchronize with my typename T
std::vector <double>::iterator It1;
std::vector <double>::iterator It1;
To copy to clipboard, switch view to plain text mode
meaning if i say
MyQueue<int> q;
MyQueue<int> q;
To copy to clipboard, switch view to plain text mode
the iterator should adjust himself to int, right now its always expecting a double.
Please help,
baray98
Bookmarks