1

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;
}
  • 5
    I don't see any overload for operator << in your vectorcollection class. – Borgleader Aug 19 '13 at 12:45
  • possible duplicate of [How to properly overload the << operator for an ostream?](http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) – Dariusz Aug 19 '13 at 12:52
  • Why are you attempting to write a `VectorCollection` class (which appears to really be a `movie` class) with a `vector` to itself (which, as noted below, won't compile)? It appears you need to separate the concerns in your design better. – Zac Howland Aug 19 '13 at 15:16
  • @Zac Howland I agree I find the assignment very confusing and think i have got it totally wrong. Do you think you could help me understand what they want and guide me on the design? – user2696346 Aug 19 '13 at 15:32
  • Without knowing the details of the assignment, you should probably start by creating a simple Movie class that contains the data elements for the movies (title, genre, rating, etc). Since you are using a `std::vector` already, there should be no need to create a collection class of your own (at most, you can typedef your `std::vector` to a `MovieCollection`). – Zac Howland Aug 19 '13 at 15:48
  • After reading your edit with the assignment, it is asking you to do several things that are HORRIBLE practices (e.g. overloading `operator*` to do non-standard behavior). You can approach it a couple ways: 1) Do what is asked to get the grade with little attention, or 2) Do it the correct way and point out to your teacher why their assignment is flawed (from both a development and academic perspective). If you take #2, you should read the books by Meyers and Sutter and reference them in your argument. – Zac Howland Aug 19 '13 at 15:52
  • @Zac Howland Dont care too much for the grade i have exam entrance already so would like to do it the correct way. Please can you guide me on the design. A simple frame work to get me going in the right direction would be great. Need to log off now. Will check your response later. Thanks Zac – user2696346 Aug 19 '13 at 16:00

3 Answers3

6

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.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
4

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?

Community
  • 1
  • 1
Dariusz
  • 21,561
  • 9
  • 74
  • 114
2

You need to provide an output stream operator for VectorCollection:

std::ostream& operator<<(std::ostream& os, const VectorCollection& v)
{
  return os << "booh!";
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480