C ++,指针的容器向量,重载运算符<<

时间:2018-05-01 19:38:29

标签: c++ vector stl

我正在尝试向控制台窗口取出指针向量,但仍无法正常工作。我可以填充矢量但是当我尝试cout<<我的对象注意到控制台窗口。

这是包含私有类成员和函数声明的头文件:

#pragma once
#include"CDealer.h"
#include<vector>
#include<fstream>
#include<iterator>
    class CShop
    {
    public:
        CShop();
        ~CShop();
        CShop(const string &fname);
        friend ostream &operator << (ostream &toStream, const CShop &S);
        friend istream &operator >> (istream &fromStream, CShop &S);

    private:
        string m_strNameShop;
        string m_strCity;
        vector<CDealer*> Dealers;

    };

这是我的构造函数,其中包含我带来数据的文件

#include "CShop.h"

CShop::CShop(){}
CShop::~CShop(){}

        CShop::CShop(const string &fname)
    {
        fstream File(fname, ios::in);
        if (File.is_open())
        {
            CDealer c;
            File >> m_strNameShop;
            File >> m_strCity;

            while (File.is_open())
            {
                File >> c;
                Dealers.push_back(new CDealer(c));
            }
            File.close();   
        }
        else
            throw "ERROR! ";
    }

这是重载运算符&lt;&lt;&lt;:

    ostream &operator << (ostream &toStream, const CShop &S)
{
    return toStream << "Name Shop: " << S.m_strNameShop << " City: " << S.m_strCity;

    vector<CDealer *>::const_iterator it = S.Dealers.begin();
    while (it != S.Dealers.end())
    {
        toStream << "Dealer " << *it++;
    }

}

所以最后我的主要

#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;   

请帮帮我:)

1 个答案:

答案 0 :(得分:2)

你在函数中有一个过早的select . . ., row_number() over (partition by jl.CustomObjectName order by (select NULL)) as columnNumber

return

删除它。

 return toStream << "Name Shop: " << S.m_strNameShop << " City: " << S.m_strCity;
 //^^^

在功能结束前添加toStream << "Name Shop: " << S.m_strNameShop << " City: " << S.m_strCity;

return

进一步改进的建议。

该行

std::ostream& operator<<(std::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())
   {
      toStream << "Dealer " << *it++;
   }

   return toStream;
}

将仅打印指针值。如果要打印指针指向的对象的详细信息,请将其更改为:

      toStream << "Dealer " << *it++;

以下街区不对。 <{1}}在此区块中始终为 Dealer* dealerPtr = *it++; toStream << "Dealer " << *dealerPtr;

File.is_open()

将其更改为:

true