簡體   English   中英

C ++,錯誤與運算符不匹配<

[英]C++, error no match for operator<<

請給我指導。我必須編寫一個程序,其中包含一個名為VectorCollection的類,該程序將用於存儲實例類型為string的向量的集合。 我收到以下錯誤

與std :: cout中的operator <<不匹配

當我嘗試使用cout << (*it) << endl;輸出矢量時cout << (*it) << endl;

我有點不願意在論壇上提出完整的作業問題,但是為了避免造成混淆。 我認為我可能完全偏離了軌道,因此任何指導都將適用。

編寫一個cpp程序,其中包含一個稱為VectorCollection的類,該類將用於存儲實例類型為string的向量的集合。 編寫一個參數構造函數,該構造函數接收用於初始化集合中值的字符串數組。 添加功能以將實例添加到集合,從集合中刪除實例,從集合中刪除所有條目並顯示整個集合。 還要覆蓋*運算符,以便它返回兩個VectorCollection對象的交集。 編寫一個程序來測試類中的所有成員函數和重載運算符。 接下來,修改main函數,以便代替為每個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類中還有一個std::vector<VectorCollection>

標准容器不能容納不完整的類型。 如果要執行此操作,則應該有一個指向VectorCollection的指針向量,或使用boost::ptr_vector專門用於保存指向類的指針。

Boost.Container也將允許您做您想做的事情。

您有一個VectorCollections的向量。 沒有為您的VectorColections對象定義<< ostream運算符。 為了能夠使用<<操作符打印您自己的類的內容,您必須為您的類正確實現它。

您可以在此處找到解決方法: 如何為ostream適當地重載<<操作符?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM