简体   繁体   中英

C++, error no match for operator<<

Please guide me with this.I have to write a program containing a class called VectorCollection that will be used to store a collection of vectors whose instances are of type string. I get the following error

no match for operator<< in std::cout

when i try to output the vector using cout << (*it) << endl;

I was a bit reluctant to put the full assignment question up on the forum but to avoid confusion here it is. I think i might be totally off track so any guidence will be appriciated.

Write a cpp program containing a class called VectorCollection that will be used to store a collection of vectors whose instances are of type string. Write a single parameter constructor that receives an array of strings for initializing values in the collection. Add functions to add instances to the collection, remove instances from the collection, delete all entries from the collection and display the entire collection. Also overlad the * operator so that it returns the intersection of two VectorCollection objects. Write a program to test all member functions and overloaded operators in your class. Next, modify the main function so that instead of creating seperate variables for each Movie object, an array of at least 4 Movie objectsis created with sample data. Loop through the array and output the name, MPAA rating and average rating for each of the four movies.

//movies.h
//******************************************************************************************************
using namespace std;


#ifndef movies_H
#define movies_H

class VectorCollection
{
    //member variables
        string newtitle,newgentre,newmpaa,newrating;
        vector<VectorCollection> movies;

    public:
        //function declarations

        //default constructor
        VectorCollection();

        //overload constructor
        VectorCollection(string,string,string,string);

        //destructor
        ~VectorCollection();

        void settitle(string);
        void setgentre(string);
        void setmpaa(string);
        void setrating(string);
        void display(vector<VectorCollection>);

};

 #endif // MOVIES_H_INCLUDED


//movies.cpp
//******************************************************************************************************
//constructor definition
VectorCollection::VectorCollection()
{

}

//overloaded constructor def
VectorCollection::VectorCollection(string title,string gentre,string mpaa,string rating)
{
    newtitle = title;
    newgentre = gentre;
    newmpaa = mpaa;
    newrating = rating;
}

//destructor def
VectorCollection::~VectorCollection()
{

}

//mutator function
void VectorCollection::settitle(string title)
{
    newtitle = title;
}

//mutator function
void VectorCollection::setgentre(string gentre)
{
    newgentre = gentre;
}

//mutator function
void VectorCollection::setmpaa(string mpaa)
{
    newmpaa = mpaa;
}

//mutator function
void VectorCollection::setrating(string rating)
{
    newrating = rating;
}

void VectorCollection::display(vector<VectorCollection> movies)
{
    vector<VectorCollection>::iterator it;

    for ( it = movies.begin(); it < movies.end(); ++it)
    {
        //copy(movies.begin(),movies.end(),ostream_iterator<string>(cout," "));
        cout << (*it) << endl;
    }

}

//main.cpp
//******************************************************************************************************
#include <iostream>
#include <string>

#include "movies.h"

using namespace std;

int main()
{
    string title,gentre,mpaa,rating;
    vector<VectorCollection> movies;
    movies.assign(4,VectorCollection());
    VectorCollection *a_movie;
    VectorCollection list;

    for (int i = 0; i < 3; i++)
    {

        cout << "Enter the title : ";
        cin >> title;
        cout << "Enter the gentre: ";
        cin >> gentre;
        cout << "Enter the MPAA: ";
        cin  >> mpaa;
        cout << "Enter the rating: ";
        cin >> rating;
        a_movie = new VectorCollection;
        a_movie ->settitle(title);
        a_movie ->setgentre(gentre);
        a_movie ->setmpaa(mpaa);
        a_movie ->setrating(rating);
        movies.push_back(*a_movie);
        cin.get();

    }

    list.display(movies);
    return 0;
}

Besides all the other problems already pointed out, you have a std::vector<VectorCollection> in your VectorCollection class.

Standard containers cannot hold incomplete types. If you want to do this, you should have a vector of pointers to VectorCollection or use boost::ptr_vector which is designed specifically to hold pointers to classes.

Boost.Container will also allow you to do what you're trying.

You have a vector of VectorCollections . There is no << ostream operator for your VectorColections object defined. To be able to print contents of your own class using << operator you have to properly implement it for your class.

You may find an answer how to do it here: How to properly overload the << operator for an ostream?

You need to provide an output stream operator for VectorCollection :

std::ostream& operator<<(std::ostream& os, const VectorCollection& v)
{
  return os << "booh!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM