I have a question about printing template class in c++ programing. Using Point2D
ID: 3676892 • Letter: I
Question
I have a question about printing template class in c++ programing. Using Point2D class, I want to print out the template class mylist as auto x:mylist, but I keep failling.
Is there any way to solve this problem without touching any classes and using Point2D print function by ostream operator <<?(One thing for sure I have to use auto x:mylist)
THe answer should in the form if you enter two integers, then save it as Point2D, and print is as if Point2D (2,3), lastly, it should print out multiple Point2Ds without (-1,-1).
example, enter 2 3 5 6 -1 -1 , then the answer should be in the form of ((2,3),(5,6)) <-not including (-1,-1)
Please Help me!!
My code is below, if you need more code, I will happy to post it
Thank you
template <class T>
class List{
public:
//constructor and destructor
List();
~List();
bool isEmpty();
//use data save it to list
void push_back(T new_value);
//instert s at iter position
Iterator<T> insert(Iterator<T> iter, T new_value);
//erase the position
Iterator<T> erase(Iterator<T> iter);
//reverse the list -problem 12.6
void reverse();
//push_front(int) -problem 12.7
void push_front(T new_value);
//swap List -problem 12.8
void swap(List<T>& other);
//get size -problem12.9-10
int get_size();
//list begin & end
Iterator<T> begin();
Iterator<T> end();
private:
Node<T>* first;
Node<T>* last;
int size;
//declare iterator class as friend
friend class Iterator<T>;
friend class Point2D;
};
template <class T>
class Node{
public:
//constructor
Node();
Node(T new_value);
private:
T data;
//declare two objects of node class next and previous pointer
Node<T> *next;
Node<T> *previous;
//declare the Iterator and class as friend
friend class Iterator<T>;
friend class List<T>;
};
template <class T>
class Iterator{
public:
Iterator();
void set(T new_value);
T operator*() const;
Iterator<T>& operator++();
Iterator<T> operator++(int unused);
Iterator<T>& operator--();
Iterator<T> operator--(int unused);
bool operator ==(Iterator<T> b)const;
bool operator!=(Iterator<T> b)const;
private:
Node<T>* position;
List<T>* container;
//declare the List as friend
friend class List<T>;
};
class Point2D {
public:
// Constructors
Point2D();
Point2D(double a, double b);
// Gets-rs
double getx();
double gety();
// Set-rs
void setx(double a);
void sety(double b);
// Print functions
virtual void print();
virtual void print(std::vector<Point2D> &point);
//sort
bool operator >(Point2D q) const;
void sort(std::vector<Point2D> &point);
private:
double x;
double y;
};
void Point2D::print() {
std::cout << "(" << x << "," << y << ")";
return;
}
int main(){
int select1 = 0,select2=0, insert=0;
List<Point2D> mylist;
Iterator<Point2D> myiterator = mylist.begin();
std::cout << "Please input a set of nonnegative numbers for a list";
std::cout << " (Enter -1 when you are finished): " << std::endl;
//if select =-1 stop the loop
while (select1 != -1&&select2 !=-1)
{
std::cin >> select1;
std::cin >> select2;
//since data is nonnegative set store the data if it is greater than 0 using push_back function
if (select1>0||select2>0)
mylist.push_back(Point2D(select1,select2));
}
//printing the mylist followed by given format;
std::cout << "Your list is"<<std::endl<<"(";
for (auto x:mylist)
std::cout << x << ",";
std::cout <<''<< ")"<<std::endl;
Explanation / Answer
You need to add two method begin() and end() which returns iterator or const_iterator.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.