2013-08-19 50 views
0

請指導我這個。我必須編寫一個程序,其中包含一個名爲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; 
} 
+5

我沒有在vectorcollection類中看到operator <<的任何重載。 – Borgleader

+0

可能重複[如何正確重載ostream的<<運算符?](http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) – Dariusz

+0

你爲什麼試圖編寫一個'VectorCollection'類(它看起來真的是一個'movie'類),並在其本身有一個'vector'(如下所述,它將不能編譯)?看起來你需要更好地分離設計中的關注點。 –

回答

4

你有一個向量VectorCollections。沒有<<ostream運營商爲您的VectorColections對象定義。爲了能夠使用<<操作員打印您自己的班級的內容,您必須爲班級正確實施它。

您可以找到答案如何在這裏做到這一點: How to properly overload the << operator for an ostream?

2

您需要爲VectorCollection提供輸出流運算符:

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

除了所有其他的問題已經指出的那樣,你有VectorCollection類中的std::vector<VectorCollection>

標準容器不能容納不完整的類型。如果你想這樣做,你應該有一個指向VectorCollection的指針向量,或者使用boost::ptr_vector,它專門用來存放指向類的指針。

Boost.Container也可以讓你做你想做的。

+0

好,我甚至沒有看到。 – juanchopanza