我有以下问题。我想为我的类创建一个文件构造函数。我正在使用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));
}
}
答案 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_type
为typedef
。
您使用std::basic_istream<CharT, Traits>
实例化std::istream_iterator
,CharT = unsigned int
不是std::ifstream
char
。为什么要这么做?省略第二个参数,默认为char
:
std::copy(std::istream_iterator<CAirtravel>(iStream),
std::istream_iterator<CAirtravel>(),
std::back_inserter(m_mAirtr));