請指導我這個。我必須編寫一個程序,其中包含一個名爲VectorCollection
的類,該類將用於存儲實例類型爲字符串的向量集合。我收到以下錯誤C++,錯誤不匹配運算符<<
性病敵不過運營商< < ::法院
當我嘗試使用輸出cout << (*it) << endl;
我有點捨不得把完整的矢量在論壇上提出質疑,但是爲了避免混淆。我想我可能完全偏離軌道,所以任何指導都會受到重視。
編寫一個包含名爲VectorCollection的類的cpp程序,該類將用於存儲實例類型爲字符串的向量集合。 編寫一個接收字符串數組的單個參數構造函數,用於初始化集合中的值。 添加函數以將實例添加到集合,從集合中刪除實例,從集合中刪除所有條目並顯示整個集合。 也覆蓋*運算符,以便它返回兩個VectorCollection對象的交集。 編寫一個程序來測試班級中的所有成員函數和重載操作符。 接下來,修改主函數,以便不用爲每個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類中看到operator <<的任何重載。 – Borgleader
可能重複[如何正確重載ostream的<<運算符?](http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) – Dariusz
你爲什麼試圖編寫一個'VectorCollection'類(它看起來真的是一個'movie'類),並在其本身有一個'vector'(如下所述,它將不能編譯)?看起來你需要更好地分離設計中的關注點。 –