How do I properly overload the << operand to produce the desired results? - c++

https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3294/
I'm following this Leetcode course on Arrays and am trying to follow along using C++ as they use Java, but I'm struggling to get past this part. I just want to read items from the pokedex array I made.
The initial error I got was:
No operator << matches these operands.
Array.cpp(16,15) operand types are std::ostream << Pokemon
I then tried to overload the << operator as I've seen other people ask on here, but I'm not getting the output I want. When I compile the program as-is, nothing prints.
Can someone explain what I can do to get the desired output?
Cyndaquill is a fire type at level 5
Also, can someone explain, or point me in the direction of someone who can explain, operator overloading in an easy non-verbose way? A lot of what I've seen on StackOverflow has been overly verbose and confusing. Maybe it's because I'm new to C++, but still.
Array.cpp
#include <iostream>
#include "Array_Header.hpp"
int main() {
Pokemon pokeDex[15];
Pokemon Cyndaquill = Pokemon("Cyndaquill", 5, "Fire");
Pokemon Totodile = Pokemon("Totodile", 5, "Water");
Pokemon Chikorita = Pokemon("Chikorita", 5, "Grass");
pokeDex[0] = Cyndaquill;
pokeDex[1] = Totodile;
pokeDex[2] = Chikorita;
std::cout << pokeDex[0];
std::cout << pokeDex[1];
std::cout << pokeDex[2];
std::cout << pokeDex[3];
}
Array_Header.hpp
#include <string>
class Pokemon {
public:
//variable declarations
std::string name;
int level;
std::string type;
//Constructor for the DVD class
Pokemon(std::string name, int level, std::string type);
Pokemon() = default;
//toString function declaration
std::string toString();
friend std::ostream& operator<<(std::ostream& os, const Pokemon& obj)
{
return os;
};
};
Array_Header.cpp
#include "Array_Header.hpp"
//Constructor Definition
Pokemon::Pokemon(std::string name, int level, std::string type){
this->type = type;
this->level = level;
this->name = name;
};
//toString function definition
std::string Pokemon::toString(){
return this->name + " is a " + this->type + " type at level " + std::to_string(level) + "\n";
};

Your operator overload for << to print to an std::ostream an object of type Pokemon does nothing but return the os parameter. You need to add the logic for printing inside of here, which would look something like this:
friend std::ostream& operator<<(std::ostream& os, const Pokemon& obj)
{
os << obj.toString();
return os;
};

Related

Properly overload << operator

I'm quite new to c++ and my current problem is to output a struct using an overloaded operator.
I've tried my best, but apparently it is not enough. Anyone knows why my compiler keeps pushing out this mistake:
\main.cpp|16|error: no match for 'operator<<' (operand types are 'std::basic_ostream' and 'const Eyecolor')|
This is the corresponding code:
#include <iostream>
#include <string>
using namespace std;
enum class Eyecolor {blue, brown, green};
struct PStruct {
string surname;
Eyecolor eyecolor;
double height;
bool gender;
friend std::ostream& operator<<(std::ostream& os, const PStruct& ps);
};
std::ostream& operator<<(std::ostream& os, const PStruct& ps)
{
os << ps.surname << '/' << ps.height << '/' << ps.gender << '/' << ps.eyecolor; //line 16
return os;
}
void print(){
cout << os;
}
int main()
{
return 0;
}
I'm pretty sure i defined the operator<< one line prior to that.
Anyway thanks for the answers in advance
A stripped down version of the error message:
no match for 'operator<<' (operand types are 'std::ostream' and 'const Eyecolor')
----------------------------> ^^
The error complains about missing operator<< for Eyecolor. The one you defined is for PStruct and tries to call the missing operator here:
os << ps.surname << '/' << ps.height << '/' << ps.gender << '/' << ps.eyecolor;
--------------------> ^^
In the same way you defined one for PStruct you need to define one for EyeColor.
Enum to string conversion is a long standing annoyance in C++, you can see how much work one has to put into it to get a generic solution here: enum to string in modern C++11 / C++14 / C++17 and future C++20. Here I am not going into that, but merely show how to get the operator<< working.
Really no offense, but making the operator a friend smells like Cargo Cult Programming. You probably have seen this in examples, and commonly the operator needs to be befriended, but there is no reason to do so in your code. Moreover print() has errors.
A fixed version could look like this:
#include <iostream>
#include <string>
enum class Eyecolor {blue, brown, green};
struct PStruct {
std::string surname;
Eyecolor eyecolor;
double height;
bool gender;
};
std::ostream& operator<<(std::ostream& os, const Eyecolor ec){
switch(ec){
case Eyecolor::blue :
os << "blue";
break;
case Eyecolor::brown :
os << "brown";
break;
case Eyecolor::green :
os << "green";
break;
default:
os << "unknown color";
}
return os;
}
std::ostream& operator<<(std::ostream& os, const PStruct& ps)
{
os << ps.surname << '/' << ps.height << '/' << ps.gender << '/' << ps.eyecolor;
return os;
}
void print(const PStruct& ps){
std::cout << ps;
}
PS:
Why is “using namespace std;” considered bad practice?

How to print out a vector which holds objects?

This question stems from a larger code I am working on but hopefully I can break it down to simplify:
Say I have two classes, one which holds 2 integer variables, and another which contains a vector that holds the first class
class TwoInts {
int num1;
int num2;
}
class NumHold {
std::vector<TwoInts> numVector;
}
How would I go about printing my numHold class exactly? I know I would have to provide an overloaded stream operator
std::ostream &operator<< (std::ostream &out, const NumHold &myNumHold){;}
But I'm a little confused as to the syntax regarding how I would print out these integers from the vector. I also understand that in order for the stream operator to work I would have to add it to my class as a friend, I just wanted to keep my NumHold class as simplified as possible for the examples sake. Hopefully this was clear enough of an explanation, any help is appreciated
Treat out how you would treat std::cout, then return it at the end of the function:
std::ostream& operator<<(std::ostream& out, const NumHold &myNumHold){
//one possible output format
for(const auto& twoInt : myNumHold.numVector){
out << twoInt.num1 << ", " << twoInt.num2 << '\n'; //pretend out is std::cout
}
return out; //don't forget to return it at the end
}
If appropriate, declare the overload as a friend function within the NumHold class:
#include<iostream>
class NumHold {
private:
std::vector<TwoInts> numVector;
public:
//friend operator overload declaration
friend std::ostream& operator<<(std::ostream& out, const NumHold& myNumHold);
};
In your example, it is appropriate, as the overload function accesses private variables.
You need to be able to access the members of each TwoInts in order to print them. One way to do this is to make them public.
class TwoInts {
public: //lets NumHold access variables to print
int num1;
int num2;
};
class NumHold {
public:
std::vector<TwoInts> numVector;
friend std::ostream& operator << (std::ostream& out, const NumHold& myNumHold) {
for (const TwoInts& ti : myNumHold.numVector) {
out << "(" << ti.num1 << ", " << ti.num2 << ")" << std::endl;
}
return out;
}
};
You can also consider adding printing methods to your classes:
#include<iostream>
class TwoInts {
private:
int num1;
int num2;
public:
void printnum1(){
std::cout << num1;
}
void printnum2(){
std::cout << num2;
}
}
class NumHold {
private:
std::vector<TwoInts> numVector;
public:
//this function prints the private attributes of the class
void printnumVector(){
for (int i = 0; i < numVector.size(); i++){
std::cout << numVector[i].printnum1() << " " << numVector[i].printnum2() << std::endl;
}
}
};

Dealing with operator overload method as a friend

I tried to create simple class and to overload some of it's operators, however, i failed at the very beginning, here's my code:
#include <iostream>
class Person
{
char *firstName;
char *lastName;
int age;
friend std::ostream &operator<<(std::ostream &, const Person &);
public:
Person() : firstName("Piotr"), lastName("Tchaikovsky"), age(10) {}
Person(char* f, char* l, int a)
{
this->firstName = f;
this->lastName = l;
age = a;
}
std::ostream &operator<<(std::ostream& out, const Person &p)
{
out << p.firstName << " " << p.lastName;
return out;
}
};
int main()
{
Person a;
getchar();
getchar();
}
So, before i created this operator overloading function i used debugger to see if constructor is going to work, and it worked, since default values were given correctly to the variable a i created, after that, all i did was that i created function that overloads the operator << and it is a friend function of my class, since i am taught that it is a good thing to do due to the type of the first parameter of overloading function, however, when i try to run this (NOTE: i have not tried to print out anything yet, i wanted to check if everything works fine first) it gives me errors saying:
"too many parameters for this operator function",
"binary 'operator <<' has too many parameters" and
" 'Person::operator<<' :error in function declaration; skipping function body"
however, i can't find any problems with function declaration, and i cannot possibly see how two parameters can be too many for this function. Any help appreciated!
You declare the friend function as a global non-member function. Then you define a member function.
Move the definition of the operator<< function to outside the class:
class Person
{
...
friend std::ostream &operator<<(std::ostream &, const Person &);
...
};
std::ostream &operator<<(std::ostream& out, const Person &p)
{
out << p.firstName << " " << p.lastName;
return out;
}
Or alternatively define the friend function inline:
class Person
{
...
friend std::ostream &operator<<(std::ostream &, const Person &)
{
out << p.firstName << " " << p.lastName;
return out;
}
...
};

C++ how to invoke a child's operator<<(ostream) from an array/collection of base elements

Say I have a Person class with a field string name. Now let's have a class Student deriving Person with a field int avgGrade. Both classes have the operator<< defined.
I want to be able to store in an array or similar structure elements of type Person, but also be able to store derived class objects in it. I'll be later traversing the elements and want to use the operator<< and actually call that specific object's definition for that operator, instead of always the base version of it.
How would I go about doing that? In what structure and what kind type of elements should I store?
Here's the current collection of classes:
Person.h:
#pragma once
#include <iostream>
class Person
{
private:
std::string name;
public:
Person();
Person(std::string);
friend std::ostream& operator<<(std::ostream& os, const Person& obj);
}
Person.cpp:
#include "Person.h"
Person::Person() : Person("default") { }
Person::Person(std::string name)
{
this->name = name;
}
std::ostream& operator<<(std::ostream& os, const Person& obj)
{
os << "Name: " << obj.name;
return os;
}
Student.h:
#pragma once
#include "Person.h"
#include <iostream>
class Student : Person
{
private:
double avgGrade;
public:
Student();
Student(const std::string cs, const double avg_grade);
friend std::ostream& operator<<(std::ostream& os, const Student& obj);
};
Student.cpp:
#include "Student.h"
Student::Student() : Student("default", 4) { }
Student::Student(const std::string cs, const double avg_grade)
: Person(cs),
avgGrade(avg_grade)
{
this->avgGrade = avg_grade;
}
std::ostream& operator<<(std::ostream& os, const Student& obj)
{
os << (Person)obj << std::endl;
os << "Average grade: " << obj.avgGrade;
return os;
}
Demo.cpp:
#include "Person.h"
#include "Student.h"
#include <iostream>
int main(int argc, char* argv[])
{
Person p("john");
Student s("johana", 5);
Person* arr[2];
arr[0] = &p;
arr[1] = &s; // Error: conversion to inaccessible base class "Person" is not allowed
std::cout << arr[0] << std::endl;
std::cout << arr[1] << std::endl;
return 0;
}
The general solution to this sort of problem is to declare:
inline std::ostream& operator<<(std::ostream& str, const Base& o)
{
o.print(str);
return str;
}
as a non-member function, then:
virtual void print(std::ostream& str);
in Base, and override in Derived as required. (The Derived version may well start with Base::print(str); to call the base version.)
Your declaration of array is fine, but you would then print the elements with:
std::cout << *arr[0] << std::endl;
std::cout << *arr[1] << std::endl;
The problem with initializing the array, is that by default, base classes are private. Fix that with:
class Student : public Person ...

How to get THIS when overloading operator in C++

I'm a student learning c++. today, I was making a operator overload function to use it in 'cout'. following is a class that contains name, coordinates, etc.
class Custom {
public:
string name;
int x;
int y;
Custom(string _name, int x, int y):name(_name){
this->x = x;
this->y = y;
}
int getDis() const {
return static_cast<int>(sqrt(x*x+y*y));
}
friend ostream& operator << (ostream& os, const Custom& other);
};
ostream& operator << (ostream& os, const Custom& other){
cout << this->name << " : " << getDis() << endl;; // error
return os;
}
However, this code isn't working because of 'THIS' keyword that I was expecting it points to the object. I want to show the object's name and distance value. How can I solve it? I think it is similar with Java's toString method so that it will be able to get THIS.
Thanks in advance for your answer and sorry for poor english. If you don't understand my question don't hesitate to make a comment.
this is available only in member functions, but your operator<< is not a class member (declaring it as friend does not make it a member). It is a global function, as it should be. In a global function, just use the arguments you are passing in:
ostream& operator << (ostream& os, const Custom& other)
{
os << other.name << " : " << other.getDis() << endl;
return os;
}
Also note os replaced cout in the code above. Using cout was an error - the output operator should output to the provided stream, not to cout always.