【问题标题】:C++, error no match for operator<<C ++,错误与运算符<<不匹配
【发布时间】:2013-08-19 12:43:25
【问题描述】:

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

在 std::cout 中不匹配运算符

当我尝试使用cout &lt;&lt; (*it) &lt;&lt; endl;输出向量时

我有点不愿意将完整的作业问题放在论坛上,但为了避免混淆,我就在这里。我想我可能完全偏离了轨道,所以任何指导都会受到影响。

编写一个包含名为 VectorCollection 的类的 cpp 程序,该类将用于存储实例为字符串类型的向量集合。 编写一个接收字符串数组的单参数构造函数,用于初始化集合中的值。 添加函数以向集合中添加实例、从集合中删除实例、从集合中删除所有条目并显示整个集合。 还覆盖了 * 运算符,以便它返回两个 VectorCollection 对象的交集。 编写一个程序来测试类中的所有成员函数和重载运算符。 接下来,修改 main 函数,以便使用示例数据创建一个包含至少 4 个 Movie 对象的数组,而不是为每个 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类中没有看到运算符
  • 你为什么要尝试编写一个 VectorCollection 类(它似乎真的是一个 movie 类)和一个 vector 本身(如下所述,它不会编译) ?看来您需要更好地分离设计中的关注点。
  • @Zac Howland 我同意我发现这项任务非常令人困惑,并认为我完全错了。你认为你能帮助我了解他们想要什么并指导我进行设计吗?
  • 在不知道分配细节的情况下,您可能应该首先创建一个包含电影数据元素(标题、流派、评级等)的简单 Movie 类。由于您已经在使用std::vector,因此不需要创建自己的集合类(最多可以将您的std::vector&lt;Movie&gt; 类型定义为MovieCollection)。

标签: c++


【解决方案1】:

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

标准容器不能容纳不完整的类型。如果你想这样做,你应该有一个指向VectorCollection 的指针向量,或者使用专门设计用于保存指向类的指针的boost::ptr_vector

Boost.Container 还可以让你做你想做的事。

【讨论】:

  • 很好,我什至没见过。
【解决方案2】:

你需要为VectorCollection提供一个输出流操作符:

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

【讨论】:

    【解决方案3】:

    你有一个VectorCollections 的向量。没有为您的 VectorColections 对象定义的 &lt;&lt; ostream 运算符。为了能够使用 &lt;&lt; 运算符打印您自己的课程的内容,您必须为您的课程正确实现它。

    您可以在此处找到如何操作的答案: How to properly overload the << operator for an ostream?

    【讨论】:

      猜你喜欢
      • 2016-03-18
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      相关资源
      最近更新 更多