overloading "<<" with a struct (no class) cout style
Asked Answered
V

5

8

I have a struct that I'd like to output using either 'std::cout' or some other output stream. Is this possible without using classes?

Thanks

#include <iostream>
#include <fstream>
template <typename T>
struct point{
  T x;
  T y;
};

template <typename T>
std::ostream& dump(std::ostream &o,point<T> p) const{
  o<<"x: " << p.x <<"\ty: " << p.y <<std::endl;
}


template<typename T>
std::ostream& operator << (std::ostream &o,const point<T> &a){
  return dump(o,a);
}


int main(){
  point<double> p;
  p.x=0.1;
  p.y=0.3;
  dump(std::cout,p);
  std::cout << p ;//how?
  return 0;
}

I tried different syntax' but I cant seem to make it work.

Vale answered 27/4, 2010 at 6:23 Comment(1)
Its worth noting that "without using classes" isn't a particularly useful qualifier in this case. There are no functional differences between a struct and a class except that structs default to public members/inheritance, while classes are private by default.Roark
S
13

Perhaps it's a copy-paste error, but there are just a few things wrong. Firstly, free-functions cannot be const, yet you have marked dump as such. The second error is that dump does not return a value, which is also easily remedied. Fix those and it should work:

template <typename T> // note, might as well take p as const-reference
std::ostream& dump(std::ostream &o, const point<T>& p)
{
    return o << "x: " << p.x << "\ty: " << p.y << std::endl;
}
Suetonius answered 27/4, 2010 at 6:28 Comment(1)
The third was that the point to be dumped wasn't marked as const. GMan has fixed that, too.Entryway
D
11

For all intents and purposes, structs are classes in C++, except that their members default to public instead of private. There are potentially minor implementation-specific differences because of optimization but these have no effect on the standard functionality which is the same for classes and structs in C++.

Secondly, why have the "dump" function? Just implement it directly in the stream operator:

template<typename T>
std::ostream& operator << (std::ostream& o, const point<T>& a)
{
    o << "x: " << a.x << "\ty: " << a.y << std::endl;
    return o;
}
Drainage answered 27/4, 2010 at 7:35 Comment(0)
S
0
#include <iostream>
#include<unordered_map>
using namespace std;
struct point{
  int x, y;
  void set(int x1,int y1){
      x = x1,y=y1;
  }
};

ostream& operator << (ostream& o, point& a)
{
    o << "x: " << a.x << "\ty: " << a.y << endl;
    return o;
}

int main() { 
     point p;
     p.set(2,3);
     point p1;
     p1.set(32,132);
     cout << p1 << p ;
    return 0;
}


Output::: 
x: 32   y: 132
x: 2    y: 3
Sill answered 28/7, 2022 at 5:40 Comment(1)
#include<unordered_map> is not required to import.Sill
S
0
#include <iostream>
#include<unordered_map>
using namespace std;
struct point{
  int x, y;
  void set(int x1,int y1){
      x = x1,y=y1;
  }
};

ostream& operator << (ostream& o, point& a)
{
    o << "x: " << a.x << "\ty: " << a.y << endl;
    return o;
}

int main() { 
     point p;
     p.set(2,3);
     point p1;
     p1.set(32,132);
     cout << p1 << p ;
    return 0;
}


Output::: 
x: 32   y: 132
x: 2    y: 3
Sill answered 5/8, 2022 at 5:58 Comment(1)
#include<unordered_map> is not required to import.Sill
A
0

If the overloading operator has to be within the struct you can use friend to define the struct as follows:

struct point{
    int x, y;
    void set(int x1,int y1){
        x = x1,y=y1;
    }
    friend std::ostream& operator<<(std::ostream& os, point const& inter)
    {
        return os << "point (" << inter.x << "," << inter.y << "," << ")\n";
    }
};
Anaheim answered 24/2, 2023 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.