473,738 Members | 7,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do u overload operator << so that cout<<A prints out the letter A as well as the value of A

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
..1 .9
..2 .4
..5 .6

currently i only have
3
2
..1 .9
..2 .4
..5 .6

so I d like to add the string A.

Sep 9 '05 #1
4 2057
bl**********@gm ail.com wrote:
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;
cout << "A\n" << A;
the following will be printed
A
3
2
.1 .9
.2 .4
.5 .6

currently i only have
3
2
.1 .9
.2 .4
.5 .6

so I d like to add the string A.


V
Sep 9 '05 #2
If you wrote the Matrix class yourself, you are going to have to give
it a "name" member variable and name it in the constructor. Then you
merely pull out this value when you use "<<". I'm not sure you can
just reference the code name of a variable at runtime since it gets
compiled into machine code and no longer your naming scheme.

Sep 9 '05 #3
What you need is a meta-information "var-name", asking the var itself.
That's possible in Java or .NET, but not standardized in C++.

Workaround:
A simple approach would be

#define COUT_W_VARNAME( var) cout << #var << var

<bl**********@g mail.com> schrieb im Newsbeitrag
news:11******** *************@o 13g2000cwo.goog legroups.com...
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
.1 .9
.2 .4
.5 .6

currently i only have
3
2
.1 .9
.2 .4
.5 .6

so I d like to add the string A.

Sep 9 '05 #4
<bl**********@g mail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
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
.1 .9
.2 .4
.5 .6


If all you need is any identification on the output, you can use the address
of the object in operator<<:

#include <iostream>

using namespace std;

class Foo
{
friend ostream & operator<< (ostream &, Foo const &);

int i_;

public:

explicit Foo(int i)
:
i_(i)
{}
};

ostream & operator<< (ostream & os, Foo const & object)
{
return os << "Foo at " << &object << ":\n"
<< object.i_;
}

int main()
{
Foo object(42);
cout << object << '\n';
}

Ali

Sep 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7170
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
7
4338
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
4
2225
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the amount my operator has for bother cin , cout operator<< and >> thanks Dan
3
2047
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;
2
2178
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;
7
14922
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
7
2796
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; ?
11
3555
by: fungus | last post by:
I have some classes which have a "writeTo()" function but no operator<<. I want to fix it so that they all have an operator<< (for consistency). Can I do something like this: template <class T> DataDest& operator<<(DataDest& d, const T& t) { t.writeTo(d);
3
1883
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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
9263
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
9208
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
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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();...
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.