简体   繁体   English

C ++,错误与运算符不匹配<

[英]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. 请给我指导。我必须编写一个程序,其中包含一个名为VectorCollection的类,该程序将用于存储实例类型为string的向量的集合。 I get the following error 我收到以下错误

no match for operator<< in std::cout 与std :: cout中的operator <<不匹配

when i try to output the vector using cout << (*it) << endl; 当我尝试使用cout << (*it) << endl;输出矢量时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. 编写一个cpp程序,其中包含一个称为VectorCollection的类,该类将用于存储实例类型为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. 还要覆盖*运算符,以便它返回两个VectorCollection对象的交集。 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. 接下来,修改main函数,以便代替为每个Movie对象创建单独的变量,而是使用示例数据创建至少4个Movie对象的数组。 Loop through the array and output the name, MPAA rating and average rating for each of the four movies. 遍历数组并输出四部电影中每部电影的名称,MPAA评分和平均评分。

//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. 除了已经指出的所有其他问题之外,您的VectorCollection类中还有一个std::vector<VectorCollection>

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. 如果要执行此操作,则应该有一个指向VectorCollection的指针向量,或使用boost::ptr_vector专门用于保存指向类的指针。

Boost.Container will also allow you to do what you're trying. Boost.Container也将允许您做您想做的事情。

You have a vector of VectorCollections . 您有一个VectorCollections的向量。 There is no << ostream operator for your VectorColections object defined. 没有为您的VectorColections对象定义<< ostream运算符。 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? 您可以在此处找到解决方法: 如何为ostream适当地重载<<操作符?

You need to provide an output stream operator for VectorCollection : 您需要为VectorCollection提供输出流运算符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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