473,804 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overoad operator<<

2 New Member
Hi, I've been trying to overload << for a function similar to boost lambda library.

I want to make for_each(v.begi n(), b.end(), cout << _1 << endl); to work, however, i'm getting a lot of problems in making this happen..

The following is my current code >

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. //Expr class to evaluate expression
  6. template <class A, class B>
  7. class Expr {
  8.  
  9. public:
  10.     A l_v;
  11.     B r_v;
  12.     string Op;
  13.     //ostream l_s;
  14.  
  15.     Expr() { }
  16.  
  17.     Expr(A l, B r, string o) {
  18.         l_v = l;
  19.         r_v = r;
  20.         Op = o;
  21.     }
  22.  
  23.     int operator()(int &);
  24. };
  25.  
  26. template <class A, class B>
  27. class ExprForOut {
  28.  
  29. public:
  30.     A l_s;
  31.     B r_s;
  32.  
  33.     ExprForOut() { }
  34.  
  35.     ExprForOut(A l, B r) {
  36.         l_s = l;
  37.         r_s = r;
  38.     }
  39.  
  40.     int operator()(int &);
  41. };
  42.  
  43. //class a2 as a placeholder/expression call for _1 
  44. class a2 {
  45.  
  46. public:
  47.     a2() {
  48.     }
  49.  
  50.     int operator()(int& x) {
  51.         return x;
  52.     }
  53.  
  54.     template <class B> Expr<a2, B> operator=(B b) {
  55.         Expr<a2, B> expression(*this, b, "=");
  56.         return expression;
  57.     }
  58.  
  59. };
  60.  
  61. int clarifyExpr(int x, int y) {
  62.     return x;
  63. }
  64.  
  65. int clarifyExpr(a2 a, int x) {
  66.     return x;
  67. }
  68.  
  69. template <class A, class B> int clarifyExpr(Expr<A,B> expression, int x) {
  70.     return eval(expression, x);
  71. }
  72.  
  73. //Evaluation of the given expression
  74. template <class A, class B> 
  75. int eval(Expr<A,B> expression, int &x) {
  76.     int var;
  77.     int l_v;
  78.     int r_v;
  79.  
  80.     if(expression.Op != "<<") {
  81.         l_v = clarifyExpr(expression.l_v, x);
  82.         r_v = clarifyExpr(expression.r_v, x);
  83.     } 
  84.  
  85.     cout<< l_v << " : " << r_v << endl;
  86.  
  87.     if(expression.Op == "+") {
  88.         var = l_v + r_v;
  89.     } else if(expression.Op == "-") {
  90.         var = l_v - r_v;
  91.     } else if(expression.Op == "/") {
  92.         var = l_v / r_v;
  93.     } else if(expression.Op == "*") {
  94.         var = l_v * r_v;
  95.     } else if(expression.Op == "=") {
  96.         x = r_v;
  97.     } 
  98.  
  99.     return var;
  100. }
  101.  
  102.  
  103. template <class A, class B>
  104. int evalOut(ExprForOut<A,B> expression, int &x) {
  105.     ostream& left = cout;
  106.  
  107.     left << x;
  108. }
  109.  
  110. template <class A, class B>
  111. int evalOut(ostream& O, int &x) {
  112.     ostream& left = O;
  113.  
  114.     left << x;
  115.     return 0;
  116. }
  117.  
  118. template <class A, class B> int Expr<A, B> :: operator()(int &x) {
  119.     return eval(*this, x);
  120. }
  121.  
  122. template <class A, class B> int ExprForOut<A,B> :: operator()(int &x) {
  123.     return evalOut(*this, x);
  124. }
  125.  
  126. template <class A, class B> Expr <A,B> operator+(A a, B b) {
  127.     Expr <A, B> expression(a, b, "+");
  128.     return expression;
  129. }
  130.  
  131. template <class A, class B> Expr <A, B> operator-(A a, B b) {
  132.     Expr <A, B> expression(a, b, "-");
  133.     return expression;
  134. }
  135.  
  136. template <class A, class B> Expr <A, B> operator*(A a, B b) {
  137.     Expr <A, B> expression(a, b, "*");
  138.     return expression;
  139. }
  140.  
  141. template <class A, class B> Expr <A, B> operator/(A a, B b) {
  142.     Expr <A, B> expression(a, b, "/");
  143.     return expression;
  144. }
  145.  
  146. template <class A, class B> ExprForOut<A, B> operator<<(A a, B b) {
  147.     cout << "Reaching here in a, b" << endl;
  148.     ExprForOut<A,B> expression(a,b);
  149.     return expression;
  150. }
  151.  
  152.  
  153. //explicit placeholder definition for _1
  154. a2 _1;

^^ The evalOut is meant to overload the function, however, whenever i put in cout, it isn't identified as ostream& type. Any suggestions? Or am I really unclear?

Thanks!
May 17 '07 #1
1 1409
ruchirp
2 New Member
just solved. don't worry about replying! it was a problem of me storing the output stream and not the reference to it!
May 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1922
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
3
2055
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
3
2962
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD FUNCTION!!
14
2343
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> using namespace std;
4
2067
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the following will be printed A 3 2
3
1596
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends; // here x contains "004400C8pluto"
2
2184
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const Debug_Level ddl;
4
2506
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
7
2805
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an object of that class. And is that also the reason why if you use class member functions for operators << and >you have to write: someclass << cout; someclass >cin; ?
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.