samedi 5 mai 2018

C++, How to read data from file and insert in container: Map

I have a problem with fill container map with information . I read the information with operator>> but it gives me these errors. no operator ">>" matches these operands and binary">>": no operator found which takes a right-hand operand of type 'const CStock'(or these is no acceptable conversion)I'll be very thankful if someone can help me.

This is my first Class CStock:

#include "CStock.h"

CStock::CStock() : m_strName_stock("n/a"), m_strProducer("n/a") {}

CStock::CStock(const string Name_, const string Prod, CDate& D)
    : m_strName_stock(Name_), m_strProducer(Prod), Date(D){}
string CStock::getName()const { return m_strName_stock; }
CDate CStock::getDate()const { return Date; }
CStock::~CStock() {}

void CStock::print() {}
void CStock::operator=(const CStock & o1)
{
    m_strName_stock = o1.m_strName_stock;
    m_strProducer = o1.m_strProducer;
}
bool operator<(const CStock & o1,const CStock & o2)
{return o1.getName() < o2.getName();}

ostream &operator << (ostream &toStream, const CStock &S){
    toStream << "Name Stock: " << S.m_strName_stock << " Producer: " << S.m_strProducer <<
        "Date: " << S.Date;

    return toStream;
}

istream &operator >> (istream &fromStream, CStock &S)
{return fromStream >> S.m_strName_stock >> S.m_strProducer >> S.Date;}

This is my second Class CDealer witch has map of object of the first class and pair map<CStock, pair<unsigned, double>> Stock;

#include "CDealer.h"
CDealer::CDealer() {}
CDealer::~CDealer() {}

CDealer::CDealer(const string Name, const string Agent, const string Addr)
    :m_strName_Dealer(Name), m_strAgent(Agent), m_strAddress(Addr) {}
void CDealer::setAddress(const string& addr)
{
    m_strAddress = addr;
}
string CDealer::getAddress() { return m_strAddress; }

bool operator==(CDealer & o1, CDealer &o2)
{
    return o1.getAddress() == o2.getAddress();

}

void CDealer::operator=(const CDealer &o1)
{
    m_strName_Dealer = o1.m_strName_Dealer;
    m_strAgent = o1.m_strAgent;
    m_strAddress = o1.m_strAddress;

}
void CDealer::operator!=(const CDealer & o1)
{
    m_strName_Dealer != o1.m_strName_Dealer;
    m_strAgent != o1.m_strAgent;
    m_strAddress != o1.m_strAddress;
}

CDealer::CDealer(const CDealer &D2)
{
    m_strName_Dealer = D2.m_strName_Dealer;
    m_strAgent = D2.m_strAgent;
    m_strAddress = D2.m_strAddress;
}
ostream &operator << (ostream &toStream, CDealer &D) 
{
    toStream << "Name Dealer: " << D.m_strName_Dealer << " Agent: " << D.m_strAgent <<
        "Address: " << D.m_strAddress;

    map<CStock, pair<unsigned, double>>::const_iterator it = D.Stock.begin();
    while (it != D.Stock.end())
    {
        toStream << it->first <<it->second.first << it->second.second;
    }
    return toStream;
}

istream &operator >> (istream &fromStream, CDealer &D)
{
    map<CStock, pair<unsigned, double>>::iterator it = D.Stock.begin();
    fromStream >> D.m_strName_Dealer >> D.m_strAgent >> D.m_strAddress;

    while (it!= D.Stock.end())
    {fromStream >> it->first >> it->second.first >> it->second.second;}
    return fromStream
}

And this is my third Class CShop

#include "CShop.h"
CShop::CShop(){}
CShop::~CShop(){}
CShop::CShop(const string &fname)
{
    CDealer c;
    fstream File(fname, ios::in);
    if (File.is_open())
    {

        File >> m_strNameShop;
        File >> m_strCity;

        while (File>>c)
        {

            Dealers.push_back(new CDealer(c));
        }
        File.close();



    }

    else
        throw "ERROR! ";
}
ostream &operator << (ostream &toStream, const CShop &S)
{
    toStream << "Name Shop: " << S.m_strNameShop << " City: " << S.m_strCity;

    vector<CDealer* >::const_iterator it = S.Dealers.begin();


    while (it != S.Dealers.end())
    {
        CDealer* dealerPtr = *it++;
        toStream << *dealerPtr<< endl;
    }
    return toStream;
}
    istream &operator >> (istream &fromStream, CShop &D)
    {
        return fromStream >> D.m_strNameShop >> D.m_strCity;

    }

And in the end main()

#include"CDealer.h"
#include"CShop.h"
#include<iostream>
#include<string>
#include <stdlib.h>  
#include<vector>
using namespace std;

int main()

{
    CShop SS1("data.txt");
    cout << SS1;
    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire