std :: vector <std :: ofstream> :: erase正在给出编译错误</std :: ofstream>

时间:2014-11-07 14:18:21

标签: c++ c++11 stdvector ofstream nsight

您好我正在尝试进行一些文件操作,如果需要,用户可以打开多个文件。我创建了一个派生的ofstream向量,但是当我想要删除i时。使用std::vector<Type>::erase(std::vector<Type>::iterator)编译器的元素(哪个文件已关闭)给了我错误,但我有另一个派生的向量std::string,我正在删除它,就像它没有给出错误一样。我的代码是;

#include <iostream>
#include <fstream>
#include <vector>

std::vector<std::ofstream> of;
std::vector<std::string> sv;

int CloseFile(int id, file_t ft) {
    std::vector<std::ofstream>::iterator i;
    std::vector<std::string>::iterator j;
    if (of[id].is_open() == true) {
        of[id].close();
        i = of.begin() + id;
        j = sv.begin() + id;
        of.erase(i); // showing error for this line
        sv.erase(j);
    }
}

我在Ubuntu 12.04上使用Nsight Eclipse Edition 6.0,我需要打开文件或文件,无论如何,eclipse输出的问题部分的错误都是;

  1. 需要'_OI std :: __ copy_move_a(_II,_II,_OI)[与bool _IsMove = false; _II = std :: basic_ofstream ; _OI = std :: basic_ofstream ]
  2. 需要'_OI std :: __ copy_move_a2(_II,_II,_OI)[与bool _IsMove = false; _II = __gnu_cxx :: __ normal_iterator *,std :: vector&gt;取代; _OI = __gnu_cxx :: __ normal_iterator *,std :: vector&gt; &GT;]
  3. 需要'_OI std :: copy(_II,_II,_OI)[与_II = __gnu_cxx :: __ normal_iterator *,std :: vector&gt;取代; _OI = __gnu_cxx :: __ normal_iterator *,std :: vector&gt; &GT;]
  4. 需要'_OI std :: copy(_II,_II,_OI)[与_II = __gnu_cxx :: __ normal_iterator *,std :: vector&gt;取代; _OI = __gnu_cxx :: __ normal_iterator *,std :: vector&gt; &GT;]
  5. std :: basic_streambuf&lt; _CharT,_Traits&gt;&amp; std :: basic_streambuf&lt; _CharT,_Traits&gt; :: operator =(const std :: basic_streambuf&lt; _CharT,_Traits&gt;&amp;)[with _CharT = char; _Traits = std :: char_traits]
  6. 的std ::的ios_base&安培; std :: ios_base :: operator =(const std :: ios_base&amp;)
  7. make:*** [dosya / dosya.o]错误1
  8. 在此内容中
  9. 在此内容中
  10. 从这里要求
  11. 指示代码行的最后一个错误

    编译器输出: (有些词是土耳其语,dosya =文件,GeriDonus_t = Return_t,DosyaKapat = FileClose)

    16:06:56 **** Incremental Build of configuration Debug for project deneme ****`
    make all`
    Building file: ../dosya/dosya.cpp`
    Invoking: NVCC Compiler`
    /usr/local/cuda-6.0/bin/nvcc -I"/usr/local/cuda-6.0/samples/3_Imaging" -I"/usr/local    /cuda-6.0/samples/common/inc" -I"/opt/Calismalar/Cuda/cuda-workspace/deneme" -I/usr/local/opencv/include -I/usr/local/opencv/include/opencv -I/opt/Calismalar/Cuda/cuda-workspace/deneme/ -G -g -O0 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_32,code=sm_32  -odir "dosya" -M -o "dosya/dosya.d" "../dosya/dosya.cpp"`
    nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.`
    /usr/local/cuda-6.0/bin/nvcc -I"/usr/local/cuda-6.0/samples/3_Imaging" -I"/usr/local/cuda-6.0/samples/common/inc" -I"/opt/Calismalar/Cuda/cuda-workspace/deneme" -I/usr/local/opencv/include -I/usr/local/opencv/include/opencv -I/opt/Calismalar/Cuda/cuda-workspace/deneme/ -G -g -O0 --compile  -x c++ -o  "dosya/dosya.o" "../dosya/dosya.cpp"
    In file included from /usr/include/c++/4.8/ios:42:0,
                     from /usr/include/c++/4.8/ostream:38,
                     from /usr/include/c++/4.8/iostream:39,
                     from ../dosya/dosya.h:13,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/bits/basic_ios.h: In instantiation of ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = std::basic_ofstream<char>*; _OI = std::basic_ofstream<char>*]’:
    /usr/include/c++/4.8/bits/stl_algobase.h:390:70:   required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = std::basic_ofstream<char>*; _OI = std::basic_ofstream<char>*]’
    /usr/include/c++/4.8/bits/stl_algobase.h:428:38:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<std::basic_ofstream<char>*, std::vector<std::basic_ofstream<char> > >; _OI = __gnu_cxx::__normal_iterator<std::basic_ofstream<char>*, std::vector<std::basic_ofstream<char> > >]’
    /usr/include/c++/4.8/bits/stl_algobase.h:460:17:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<std::basic_ofstream<char>*, std::vector<std::basic_ofstream<char> > >; _OI = __gnu_cxx::__normal_iterator<std::basic_ofstream<char>*, std::vector<std::basic_ofstream<char> > >]’
    /usr/include/c++/4.8/bits/vector.tcc:138:2:   required from ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp = std::basic_ofstream<char>; _Alloc = std::allocator<std::basic_ofstream<char> >; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::basic_ofstream<char>*, std::vector<std::basic_ofstream<char> > >; typename std::_Vector_base<_Tp, _Alloc>::pointer = std::basic_ofstream<char>*]’
    ../dosya/dosya.cpp:152:27:   required from here
    /usr/include/c++/4.8/bits/ios_base.h:789:5: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private
         operator=(const ios_base&);
         ^
    In file included from /usr/include/c++/4.8/ios:44:0,
                     from /usr/include/c++/4.8/ostream:38,
                     from /usr/include/c++/4.8/iostream:39,
                     from ../dosya/dosya.h:13,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/bits/basic_ios.h:66:11: error: within this context
         class basic_ios : public ios_base
               ^
    In file included from /usr/include/c++/4.8/iostream:39:0,
                     from ../dosya/dosya.h:13,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/ostream:58:11: note: synthesized method ‘std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>&)’ first required here 
         class basic_ostream : virtual public basic_ios<_CharT, _Traits>
               ^
    In file included from ../dosya/dosya.h:14:0,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/fstream:599:11: note: synthesized method ‘std::basic_ostream<char>& std::basic_ostream<char>::operator=(const std::basic_ostream<char>&)’ first required here 
         class basic_ofstream : public basic_ostream<_CharT,_Traits>
               ^
    In file included from /usr/include/c++/4.8/ios:43:0,
                     from /usr/include/c++/4.8/ostream:38,
                     from /usr/include/c++/4.8/iostream:39,
                     from ../dosya/dosya.h:13,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/streambuf:810:7: error: ‘std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
           operator=(const basic_streambuf&) { return *this; };
           ^
    In file included from ../dosya/dosya.h:14:0,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/fstream:72:11: error: within this context
         class basic_filebuf : public basic_streambuf<_CharT, _Traits>
               ^
    /usr/include/c++/4.8/fstream:599:11: note: synthesized method ‘std::basic_filebuf<char>& >std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’ first required here 
         class basic_ofstream : public basic_ostream<_CharT,_Traits>
               ^
    In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                     from /usr/include/c++/4.8/ios:40,
                     from /usr/include/c++/4.8/ostream:38,
                     from /usr/include/c++/4.8/iostream:39,
                     from ../dosya/dosya.h:13,
                     from ../dosya/dosya.cpp:9:
    /usr/include/c++/4.8/bits/stl_algobase.h:335:18: note: synthesized method ‘std::basic_ofstream<char>& std::basic_ofstream<char>::operator=(const std::basic_ofstream<char>&)’ first required here 
            *__result = *__first;
                      ^
    make: *** [dosya/dosya.o] Error 1
    
    16:06:57 Build Finished (took 456ms)`
    

    我根本不知道。我还查看了thisthisthisthis(这是关于日食bug),FAQ - q7.1, this(关于列表,但错误是相同的,并说错误实际上不是错误,我没有更改编译器(nvcc))。谢谢大家。

3 个答案:

答案 0 :(得分:2)

Gcc-4.8不支持移动流。在这方面,您的代码符合C ++ 11,只是您的实现不是。

答案 1 :(得分:1)

std :: vector将其元素保存在连续的内存块中,没有间隙。擦除元素会导致后面的所有元素被移动或复制到新位置。在这种情况下,std :: ofstream无法移动,因此它会在编译错误中运行。

不要在std :: vector中直接使用std :: ofstream。它适用于引用,指针或 - 我首选 - std :: unique_ptr。

它也适用于std :: list而不是vector。 std :: list有另一种方法来保存它的元素,它不会移动它们。但我不确定这是否比std:vector中的reference / pinter / std_unique_ptr更好。

答案 2 :(得分:-2)

我已经更改了代码,现在没有错误。谢谢大家。

现在是:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

std::vector<std::ofstream*> of;
std::vector<std::string> sv;

int CloseFile(int id, file_t ft) {
    std::vector<std::ofstream*>::iterator i;
    std::vector<std::string>::iterator j;
    if (of[id]->is_open() == true) {
        of[id]->close();
        i = of.begin() + id;
        j = sv.begin() + id;
        free(of[id]);
        of.erase(i);
        sv.erase(j);
    }
}

但我还没试过。现在没有编译器错误。