没有构造函数std :: istream_iterator的实例

时间:2016-03-13 12:33:15

标签: c++ istream istream-iterator

我有以下问题。我想为我的类创建一个文件构造函数。我正在使用std::copy算法,我遇到了这种错误:

No instance of constructor std::istream_iterator ... matches the argument list
error C2440: <function-style-cast> cannot convert from std::ifsteam to std::istream_operator

这是我的构造函数:

CAirport(const char* FileName, const string& name, const int& num)
{
    m_strName = name;
    m_uNum = num;
    ifstream iStream(FileName);

    if(iStream.good())
    {
        copy(istream_iterator<CAirtravel, unsigned>(iStream),
             istream_iterator<CAirtravel, unsigned>(),
             back_inserter(m_mAirtr));
    }
}

1 个答案:

答案 0 :(得分:0)

std::istream_iterator的构造函数需要Traceback (most recent call last): File "<ipython-input-17-528eea6bbfdd>", line 1, in <module> runfile('C:/Users/MAC/Documents/Python Scripts/untitled0.py', wdir='C:/Users/MAC/Documents/Python Scripts') File "C:\Users\MAC\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile execfile(filename, namespace) File "C:\Users\MAC\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/MAC/Documents/Python Scripts/untitled0.py", line 19, in <module> train_set, valid_set, test_set = cPickle.load(f) File "C:\Users\MAC\Anaconda\lib\gzip.py", line 268, in read self._read(readsize) File "C:\Users\MAC\Anaconda\lib\gzip.py", line 320, in _read self._add_read_data( uncompress ) File "C:\Users\MAC\Anaconda\lib\gzip.py", line 338, in _add_read_data self.extrabuf = self.extrabuf[offset:] + data MemoryError istream_typetypedef

您使用std::basic_istream<CharT, Traits>实例化std::istream_iteratorCharT = unsigned int不是std::ifstream char。为什么要这么做?省略第二个参数,默认为char

std::copy(std::istream_iterator<CAirtravel>(iStream),
          std::istream_iterator<CAirtravel>(),
          std::back_inserter(m_mAirtr));