在C++重载cout ostream操作员

overloading cout ostream operator in C++

本文关键字:ostream 操作员 cout 重载 C++      更新时间:2023-10-16

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

有没有办法在不接触任何类并使用 ostream 运算符<<的 Point2D 打印功能的情况下解决此问题?(Auto X:Mylist是强制性的)

如果您输入两个整数,答案应该在形式中,然后将其另存为 Point2D,打印就像 Point2D (2,3) 一样,最后,它应该打印出多个没有 (-1,-1) 的 Point2D。

例如,如果我输入 2 3 5 6 -1\

n -1,则答案应采用 (((2,3),(5,6)) <-不包括 (-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;
}

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

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

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

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

非常

建议更改您的 print(),因此使用 ostream:

virtual void print(ostream& os);