繁体   English   中英

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

[英]C++, error no match for operator<<

请给我指导。我必须编写一个程序,其中包含一个名为VectorCollection的类,该程序将用于存储实例类型为string的向量的集合。 我收到以下错误

与std :: cout中的operator <<不匹配

当我尝试使用cout << (*it) << endl;输出矢量时cout << (*it) << endl;

我有点不愿意在论坛上提出完整的作业问题,但是为了避免造成混淆。 我认为我可能完全偏离了轨道,因此任何指导都将适用。

编写一个cpp程序,其中包含一个称为VectorCollection的类,该类将用于存储实例类型为string的向量的集合。 编写一个参数构造函数,该构造函数接收用于初始化集合中值的字符串数组。 添加功能以将实例添加到集合,从集合中删除实例,从集合中删除所有条目并显示整个集合。 还要覆盖*运算符,以便它返回两个VectorCollection对象的交集。 编写一个程序来测试类中的所有成员函数和重载运算符。 接下来,修改main函数,以便代替为每个Movie对象创建单独的变量,而是使用示例数据创建至少4个Movie对象的数组。 遍历数组并输出四部电影中每部电影的名称,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;
}

除了已经指出的所有其他问题之外,您的VectorCollection类中还有一个std::vector<VectorCollection>

标准容器不能容纳不完整的类型。 如果要执行此操作,则应该有一个指向VectorCollection的指针向量,或使用boost::ptr_vector专门用于保存指向类的指针。

Boost.Container也将允许您做您想做的事情。

您有一个VectorCollections的向量。 没有为您的VectorColections对象定义<< ostream运算符。 为了能够使用<<操作符打印您自己的类的内容,您必须为您的类正确实现它。

您可以在此处找到解决方法: 如何为ostream适当地重载<<操作符?

您需要为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