【发布时间】:2013-08-19 12:43:25
【问题描述】:
请指导我。我必须编写一个程序,其中包含一个名为VectorCollection 的类,该类将用于存储实例为字符串类型的向量集合。我收到以下错误
在 std::cout 中不匹配运算符
当我尝试使用cout << (*it) << 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<Movie>类型定义为MovieCollection)。
标签: c++