请指导我。我必须编写一个程序,其中包含一个名为的类,该类VectorCollection
将用于存储实例为字符串类型的向量集合。我收到以下错误
与 std::cout 中的 operator<< 不匹配
当我尝试使用输出向量时cout << (*it) << endl;
我有点不愿意将完整的作业问题放在论坛上,但为了避免混淆,它就在这里。我想我可能完全偏离了轨道,所以任何指导都会受到影响。
编写一个包含名为 VectorCollection 的类的 cpp 程序,该类将用于存储实例为字符串类型的向量集合。编写一个接收字符串数组的单参数构造函数,用于初始化集合中的值。添加函数以向集合中添加实例、从集合中删除实例、从集合中删除所有条目并显示整个集合。还覆盖了 * 运算符,以便它返回两个 VectorCollection 对象的交集。编写一个程序来测试类中的所有成员函数和重载运算符。接下来,修改 main 函数,以便使用示例数据创建一个包含至少 4 个 Movie 对象的数组,而不是为每个 Movie 对象创建单独的变量。遍历数组并输出名称,
//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;
}