用C ++重载cout ostream操作符

时间:2016-03-11 21:21:14

标签: c++ cout ostream

我有一个关于在c ++编程中打印模板类的问题。使用Point2D类,我想打印模板类mylist为auto x:mylist,但我一直都失败了。

有没有办法解决这个问题而不触及任何类并使用ostream operator<<<<< ? (自动x:mylist是强制性的)

如果您输入两个整数,则应该在表单中给出答案,然后将其保存为Point2D,并且打印就像Point2D(2,3)一样,最后,它应打印多个Point2D而不使用(-1,-1)。

例如,如果我输入2 \ n 3 \ n 5 \ n 6 \ n -1 \ n -1 \ n,则答案应采用((2,3),(5,6)的形式))< -not included(-1,-1)

请帮助我!!

#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <conio.h>

//declaration for Node, Iterator class;
template <class T>
class Node;
template <class T>
class Iterator;
class Point2D;

template <class T>
class List{
public:
    //constructor and destructor
    List();
    ~List();
    //list begin & end
    Iterator<T> begin();
    Iterator<T> end();
private:
    Node<T>* first;
    Node<T>* last;
    int size;

   friend class Iterator<T>;
   friend class Point2D;
   friend std::ostream& operator<<(std::ostream& os, const List<T>& list);
};

 template <class T>
 class Iterator{
 public:
    Iterator();
    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);
    // Print functions
    virtual void print();
private:
    double x;
    double y;
};


template <class T>
List<T>::List(){
     //initialization
     first=NULL;
     last=NULL;
    size=0; 
 }

 template <class T>
 List<T>::~List(){
    delete first;
    delete last;
    delete size;
 }

//check begin()
template <class T>
Iterator<T> List<T>::begin(){
    Iterator<T> iter;
    iter.position=first;
    iter.container=this;
    return iter;
 }

//check end()
template <class T>
Iterator<T> List<T>::end(){
    Iterator<T> iter;
    iter.position=NULL;
    iter.container=this;
    return iter;
 }

 //check begin()
 template <class T>
 Iterator<T> List<T>::begin(){
     Iterator<T> iter;
     iter.position=first;
     iter.container=this;
     return iter;
 }
 //check end()
 template <class T>
 Iterator<T> List<T>::end(){
    Iterator<T> iter;
    iter.position=NULL;
    iter.container=this;
    return iter;
 }

 //Iterator operator * -> pointing the position
 template <class T>
 T Iterator<T>::operator*()const
  {
      assert(position !=NULL);
      return position->data;
  }

 //pre increment for operator++
 template <class T>
      Iterator<T>& Iterator<T>::operator++() {
      assert(position!=NULL);
      position=position->next;
      return* this;
 }
 //post increment for operator++
 template <class T>
 Iterator<T> Iterator<T>::operator++(int unused) {
    assert(position != NULL);
    auto clone(*this);
    ++(*this);
    return clone;
 }
 //pre increment for operator--
 template <class T>
 Iterator<T>& Iterator<T>::operator--() {
      assert(position!=container->first);
      if(position==NULL) position =container->last;
      else position=position->previous;
      return* this;
 }
 //post increment for operator--
 template <class T>
 Iterator<T> Iterator<T>::operator --(int unused){
     auto clone(*this);
         --(*this);
         return clone;
 } 
 //boolean operator to check ==
 template <class T>
 bool Iterator<T>::operator ==(Iterator<T> b)const{
 return position==b.position;
 }
 //boolean operator to check !=
 template <class T>
 bool Iterator<T>::operator!=(Iterator<T> b)const{
    return position!=b.position;
 }

 Point2D::Point2D() { x = 0; y = 0; return; }
 Point2D::Point2D(double a, double b) { x = a; y = b; return; }

 void Point2D::print() {
    std::cout << "(" << x << "," << y << ")";
    return;
 }

int main(){
 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 select1 &2=-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 
      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 <<'\b'<< ")"<<std::endl;
}

1 个答案:

答案 0 :(得分:1)

您总是可以在所有类之外编写一个ostream运算符:

ostream& operator<< (ostream& os, Point2D p) {
   p.print(); 
   return os; 
}

然后你可以通过cout<< p<<endl来调用它;

在您的情况下,问题是print()特定于输出流,而<<独立于ostream。因此,后者可能用于字符串流或文件流,但实现错误,因此如果有一天,您尝试输出到文件而不是控制台,则此代码将继续在控制台上打印而不会出现预期输出目的地为空。

更改你的print()是非常非常可取的,所以要使用ostream:

virtual void print(ostream& os);