473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 2034
bl**********@gmail.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**********@gmail.com> schrieb im Newsbeitrag
news:11*********************@o13g2000cwo.googlegro ups.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**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.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
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): ...
7
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; }...
4
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...
3
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...
2
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...
7
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...
7
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...
11
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...
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.