MrRobot
MrRobot

Reputation: 57

overloading operator << for a pointer such overloaing is not avalible in mina

I have two child class called Episode & Movie, in the father class called Video I overload the operator <<. I need to print Movie or Episode objects (all with the same format). I'm going to do polymorphism, so I'm using a vector<Video*>. I have done this before but not with pointers; the problem here is the Syntaxis (most likely). I couldn't find any example for overloading operator<< when the input is a pointer so here I'm... help pls.

Note: I'm barely starting my code, but since operator overloading is new to me is the 1st thing I'm doing. Thus the Episode files are basically empty and the movie files are exactly the same as Episode (at this moment). At this moment I'm just making sure child classes inherit the overloading operator<< with no extra code and things like that.

error:

"message": "no operator \"<<\" matches these operands -- operand types are: std::ostream << std::vector<Video *, std::allocator<Video *>>"

Video.h

#ifndef VIDEO_H
#define VIDEO_H
#include <string>
#include <iostream>

class Video
{
    public:
    Video();
    ~Video();
    void updateRate(int);
    std::string getName();
    std::string getGenre();
    double getTime();
    int getId();
    int getRate();
    int getNumVotes();

    friend std::ostream& operator<<(std::ostream& output, Video& vdeo)
        {
            output << vdeo.getName() << " (" << vdeo.getId() << ") " << vdeo.getTime() << "minutes     ";
            output << vdeo.getRate() << " ★   " << vdeo.getNumVotes() << "Reviews" << std::endl;

            return output;
        }

    private:
        void setName(std::string);
        void setGenre(std::string);
        void setTime(double);
        void setId(int);

    protected:
        std::string name,
                    genre;
        double time;
        int id,
            rate,
            numVotes;
};

#endif

Video.cpp

#include <iostream>
#include "Video.h"

Video::Video()
{

}

Video::~Video()
{

}

void Video::setName(std::string name)
{
    this->name = name;
}

void Video::setGenre(std::string genre)
{
    this->genre = genre;
}

void Video::setTime(double time)
{
    if(time > 0)
        this->time = time;
    else
        std::cout << "Time cannot be zero or less" << std::endl;
}

void Video::setId(int id)
{
    this->id = id;
}

void Video::updateRate(int vote)
{
    int sum = (rate * numVotes) + vote;

    numVotes++;
    rate = sum / numVotes;
}

std::string Video::getName()
{
    return name;
}

std::string Video::getGenre()
{
    return genre;
}

double Video::getTime()
{
    return time;
}

int Video::getId()
{
    return id;
}

int Video::getRate()
{
    return rate;
}

int Video::getNumVotes()
{
    return numVotes;
}

Episode.h

#ifndef EPISODE_H
#define EPISODE_H
#include <iostream>
#include "Video.h"

class Episode : public Video
{
    public:
        Episode(int, double, std::string, std::string);
        ~Episode();

    private:

};

#endif

Episode.cpp

#include "Episode.h"

Episode::Episode(int id, double time, std::string name, std::string genre)
{

}

Episode::~Episode()
{

}

main.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include "Video.h"
#include "Episode.h"


using namespace std;

int main()
{

    vector<Video*> vdeo;

    vdeo.push_back(new Episode(4, 12.5, "nombre", "Atzion"));

    cout << vdeo;
}

Upvotes: 0

Views: 40

Answers (2)

letsShareKnowledge
letsShareKnowledge

Reputation: 57

To accomplish what you want to display try cout<<*(vdeo[0]) because, vdeo is a vector containing pointer to a Video(episode in this case), and if you write cout<<vdeo[0] you will get some hexadecimal number which is address of the memory you have allocated using new operator. Hence you can derefer vdeo[0].

Upvotes: 1

john
john

Reputation: 87957

Not completely sure what you want to do but try this

for (auto vp : vdeo)
    cout << *vp;

All you need to do is use * to turn your pointer into a reference and then you can use the operator<< overloaded for Video.

Upvotes: 1

Related Questions