1

VectorCollectionこれを教えてください。インスタンスが文字列型であるベクトルのコレクションを格納するために使用されるクラスを含むプログラムを作成する必要があります。次のエラーが表示されます

std::cout の operator<< に一致しません

を使用してベクトルを出力しようとするとcout << (*it) << endl;

課題の全文をフォーラムに投稿するのは少し気が進まなかったのですが、混乱を避けるためにここに投稿しました。私は完全に軌道から外れている可能性があると思うので、ガイダンスがあれば感謝します。

インスタンスが文字列型のベクトルのコレクションを格納するために使用される VectorCollection というクラスを含む cpp プログラムを作成します。コレクション内の値を初期化するための文字列の配列を受け取る単一パラメーター コンストラクターを記述します。コレクションへのインスタンスの追加、コレクションからのインスタンスの削除、コレクションからのすべてのエントリの削除、およびコレクション全体の表示を行う関数を追加します。また、* 演算子をオーバーレイして、2 つの VectorCollection オブジェクトの共通部分を返すようにします。クラス内のすべてのメンバー関数とオーバーロードされた演算子をテストするプログラムを作成します。次に、main 関数を変更して、各 Movie オブジェクトに対して個別の変数を作成する代わりに、サンプル データを使用して少なくとも 4 つの 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;
}
4

3 に答える 3

6

すでに指摘されている他のすべての問題に加えて、std::vector<VectorCollection> あなたのVectorCollectionクラスには があります。

標準コンテナは不完全な型を保持できません。これを行うには、クラスへのポインタを保持するように特別に設計されたポインタのベクトルVectorCollectionまたは使用が必要です。boost::ptr_vector

Boost.Containerまた、あなたがしようとしていることができるようになります。

于 2013-08-19T12:50:37.157 に答える
4

のベクトルがありVectorCollectionsます。<< ostreamオブジェクトの演算子がVectorColections定義されていません。<<演算子を使用して独自のクラスのコンテンツを出力できるようにするには、クラスに適切に実装する必要があります。

ここでそれを行う方法の答えを見つけることができます: ostream の << 演算子を適切にオーバーロードする方法は?

于 2013-08-19T12:46:33.353 に答える
2

次の出力ストリーム演算子を提供する必要がありますVectorCollection

std::ostream& operator<<(std::ostream& os, const VectorCollection& v)
{
  return os << "booh!";
}
于 2013-08-19T12:48:23.360 に答える