C++: Printing out map values - c++

So I have a map like this:
map<long, MusicEntry> Music_Map;
MusicEntry contains strings of (name, artist, size, and date added)
My question is how can print, how do I print out all the data in my map? I have tried doing...
for(auto it = Music_Map.cbegin(); it!= Music_Map.cend(); ++it)
cout << it-> first << ", " << it-> second << "\n";
I think the problem is occuring that it can't compile and read second aka MusicEntry..

You need to provide an std::ostream operator<< (std::ostream&, const MusicEntyr&) so that you can do this kind of thing:
MusicEntry m;
std::cout << m << std::endl;
With that in place, you can print the map's second fields. Here's a simplified example:
struct MusicEntry
{
std::string artist;
std::string name;
};
std::ostream& operator<<(std::ostream& o, const MusicEntry& m)
{
return o << m.artist << ", " << m.name;
}

The code you have is fine, but you need to implement
std::ostream& operator<<(ostream& os, const MusicEntry& e)
{
return os << "(" << e.name << ", " << ... << ")";
}
and you probably need to declare the above a friend in MusicEntry to access the private (or protected) data of MusicEntry:
class MusicEntry
{
// ...
friend std::ostream& operator<<(ostream& os, const MusicEntry& e);
};
This is, of course, not needed if the data is public or if you use public getters. You can find a lot more information in the operator overloading FAQ.

Related

c++ overloaded << prints adress of object attribute, instead of actual value

So I am learning operation overloading.
I am trying to overload << to print object properties.
In my case Properties is a class that inherits the public section of the person class.
This is what i have:
class Person {
private:
string name;
int egn;
string adress;
public:
friend ostream& operator << (ostream& out, const Person& c);
}
ostream& operator << (ostream& out, const Person& c)
{
out << "Person name: " << c.name << "\n" << "Person egn: " << c.egn;
return out;
}
int main()
{
Properties* person_obj_1 = new Properties();
person_obj_1 = add_person(person_obj_1);//Add some values
cout << person_obj_1 << "\n";
}
And the output I get is 000001CE89252130. This is not what I want. I need to print the actual values of my attributes. What am I doing wrong?
What am I doing wrong?
You're printing a Properties* instead of a Person object. That is, to get your expected result you need to use cout on a Person object for which you've overloaded operator<< instead of Properties* which is a pointer.

overloading operator << c++, I am trying to cout the element of class

I am writing this code
ostream& operator <<(ostream& out, Box& B){
return B.l +" "+B.b +" "+B.h +endl;
};
The error I get is
Solution.cpp:40:46: error: ‘std::ostream& Box::operator<<(std::ostream&, Box&)’ must have exactly one argument ostream& operator <<(ostream& out, Box& B){ ^
can someone explain what's wrong? I dont understand.
thanks for your help :)
It seems you mean the following
std::ostream & operator <<( std::ostream& out, const Box& B) {
return out << B.l << " " << B.b << " " << B.h;
}
provided that all used in the operator data members are public data members of the class Box. The operator shall be declared and defined outside the class definition.
If one of the used data members is a private data member of the class then the function should be a friend function of the class and shall be declared (and may be defined) in the class definition. For example
class Box
{
//...
friend std::ostream & operator <<( std::ostream& out, const Box& B) {
return out << B.l << " " << B.b << " " << B.h;
}
//...
};
Pay attention to that it is better to remove in the return statement the operand std::endl. In this case 1) the operator will be more flexible because you can output additional information in the same line and 2) this statement
std::cout << box;
will not confuse readers of the code because they will not see the operand std::endl.
Without this operand in the operator definition in the caller of the operator you can write
std::cout << box << std::endl;
and this statement more clear expresses the intention of the programmer.
It should probably be:
std::ostream& operator<<(std::ostream& out, Box const& B){
out << B.l << " " << B.b << " " << B.h << std::endl;
return out;
};
Full code should look like:
#include <iostream>
class Box {
int l;
int b;
int h;
friend std::ostream& operator<<(std::ostream& out, Box const& B);
};
std::ostream& operator<<(std::ostream& out, Box const& B){
out << B.l << " " << B.b << " " << B.h << std::endl;
return out;
}
int main() {
return 0;
}
Demo
To output a type T to std::ostream, you have to declare standalone operator, which returns reference to that stream
std::ostream& operator<<(std::ostream& out, const T& B)
{
out << B.l << " " << B.b << " " << B.h << '\n';
return out;
}
This operation cannot be member operator because member operator use their class as first argument, therefore it might be necessary declare it as a friend of class T.
Avoid using endl in such operators if not extremely necessary, that manipulator calls out.flush(). If you want new line added, use new-line character.

C++ How do I print an object?

I have created a constructor
Location(double xIn,double yIn,string placeIn,int timeIn)
: x(xIn),y(yIn) ...so on {
Say I want to print Location home(x,y,place,time); that's in the main().
How would I do so? I've been searching around and was told to use operator<<. How would I implement this?
UPDATE: After creating some get methods and I tried doing,can't exactly compile it because of the problem
ostream &operator<<(ostream & o, const Location & rhs){
o << rhs.getX() << "," << rhs.getY() << "," << rhs.getPlace() << "," << rhs.getTime();
return o; }
Here is the stencil for overloading operator<<:
class Any
{
public:
friend std::ostream& operator<<(std::ostream& output, const Any& a);
private:
int member;
};
std::ostream&
operator<<(std::ostream& output, const Any& a)
{
output << a.member;
return output;
}
This one possible stencil, there are other possibilities. Search the internet for "c++ stream insertion operator overload example" for other implementations.

c++: print string vector returned by function

So essentially I have a private std::vector<std::string> alloc in class A which has some manipulation etc done on it by public method std::vector<std::string> allocMethod() and this method then does return alloc. The method is called in class B
In class B I want to print the return value of allocMethod, i.e the alloc vector. This is what I have
std::cout << A.allocMethod();
I know that normally one would use some sort of interator and loop to print out a vector but I don't think I can do that as it is a functions return value being printed not actually a vector.
Note: I reeaaally don't wan't to have to just print out the vector in class A.
Is there a way to output the std::vector<string> result as shown above?
"Note: I reeaaally don't wan't to have to just print out the vector in class A."
As you want to use this repeatedly it might be worth it to provide an appropriate output operator overload for std::vector<std::string>
std::ostream& operator<<(std::ostream& os,const std::vector<std::string>>& vs) {
for (const auto& s : vs) {
os << s << " ";
}
return os;
}
That should allow for just calling
std::cout << A.allocMethod();
You can even try a more generic overload
template<typename T>
std::ostream& operator<<(std::ostream& os,const std::vector<T>>& vs) {
for (const auto& s : vs) {
os << s << " ";
}
return os;
}
Contrary to your believe, you can print the vector by iterating over it although it is a temporary using the C++11 for-loop like this:
for (const auto& i : A.allocMethod())
std::cout << i << '\n';
You can just store the return value and then iterate over it. Like this:
std::vector<std::string> result = allocMethod();
for (std::size_t i = 0; i < result.size(); ++i)
{
std::cout << result[i];
}

<< operator overloading as a class method in c++

I want to overload the << operator for my class Complex.
The prototype is the following:
void Complex::operator << (ostream &out)
{
out << "The number is: (" << re <<", " << im <<")."<<endl;
}
It works, but I have to call it like this: object << cout for the standart output.
What can I do to make it work backwards, like cout << object?
I know that the 'this' pointer is by default the first parameter sent to the method so that's why the binary operator can work only obj << ostream. I overloaded it as a global function and there were no problems.
Is there a way to overload the << operator as a method and to call it ostream << obj?
I would just use the usual C++ pattern of free function. You can make it friend to your Complex class if you want to make Complex class's private data members visible to it, but usually a complex number class would expose public getters for real part and imaginary coefficient.
class Complex
{
....
friend std::ostream& operator<<(std::ostream &out, const Complex& c);
private:
double re;
double im;
};
inline std::ostream& operator<<(std::ostream &out, const Complex& c)
{
out << "The number is: (" << c.re << ", " << c.im << ").\n";
return out;
}
You could write a free stand operator<< function, try:
std::ostream& operator<< (std::ostream &out, const Complex& cm)
{
out << "The number is: (" << cm.re <<", " << cm.im <<")." << std::endl;
return out;
}
You can define a global function:
void operator << (ostream& out, const Complex& complex)
{
complex.operator<<(out);
}