2012-07-08 41 views
-7

为代码量提前道歉,但我找不到从哪里来的内置错误。此示例在VC++ 6.0上正确构建,但在最近的编译器(包括g ++和VC++ 2010)上失败。错误消息显示错误C2248:'std :: basic_ios < _Elem,_Traits> :: basic_ios':无法访问在'std :: basic_ios < _Elem,_Traits>'请帮助。STL算法无法访问私有成员

#pragma warning(disable : 4786) 
    #pragma warning(disable : 4996) 

    #include <iostream> 
    #include <fstream> 
    #include <algorithm> 
    #include <vector> 
    #include <list> 
    #include <numeric> 
    #include <iterator> 

    template<class T> 
    class Criteria { 
    public: 
    Criteria(T match) : m_match(match) { 

    } 

    bool operator()(T match) { 
     return (m_match == match); 
    } 
    private: 
    T m_match; 
    }; 

    template<class T> 
    class BinaryCriteria { 
    public: 
    bool operator()(T item1, T item2) { 
     return (item1*item1 > item2+item2); 
    } 
    }; 

    std::ostream& operator<<(std::ostream& o, std::string rhs) { 
    return (o << rhs.c_str()); 
    } 

    void print(int x) { 
    std::cout << x << " "; 
    } 

    template<class T> 
    class Print { 
    public: 
    Print(const char* sep) : m_sep(sep) { 
    } 
    void operator()(T item) { 
     std::cout << item << m_sep.c_str(); 
    } 
    private: 
    std::string m_sep; 
    }; 

    template<class T> 
    class Equal { 
    public: 

    bool operator()(T item1, T item2) { 
     return (item1 * 2 == item2); 
    } 
    }; 

    template<class T> 
    class File { 
    public: 
    File(const char* file) { 
     m_in.open(file); 
    } 
    ~File() { 
     if (m_in) 
      m_in.close(); 
    } 
    T operator()() { 
     m_in.getline(m_buf, sizeof(m_buf)); 
     T line = m_buf; 
     return line; 
    } 
    private: 
    std::ifstream m_in; 
    char m_buf[80]; 
    }; 

    template<class T> 
    class Odd { 
    public: 
    bool operator()(T item) { 
     if (item % 2 != 0) 
      return false; 
     else 
      return true; 
    } 
    }; 

    template<class T> 
    class Upper { 
    public: 
    T operator()(T item) { 
     std::transform(item.begin(),item.end(),item.begin(),::toupper); 
     return item; 
    } 
    }; 

    template<class T> 
    class Concat { 
    public: 
    Concat(const char* sep) : m_sep(sep) { 

    } 
    T operator()(T item1, T item2) { 
     T out; 
     out = item1; 
     out += m_sep; 
     out += item2; 
     return out; 
    } 
    private: 
    std::string m_sep; 
    }; 

    template<class T> 
    class TheSame { 
    public: 
    bool operator()(T item1, T item2) { 
     if (item1 * 10 == item2) 
      return true; 
     else 
      return false; 
    } 
    }; 

    template<class T> 
    class Rand { 
    public: 
    T operator()() { 
     return static_cast<T>(::rand()); 
    } 
    }; 

    struct Employee { 
    Employee(std::string name, std::string dept, std::string id) : 
      m_name(name), m_dept(dept), m_id(id) { 
    } 
    std::string m_name; 
    std::string m_dept; 
    std::string m_id; 
    }; 

    class SortByName { 
    public: 
    bool operator()(const Employee* lhs, const Employee* rhs) { 
     int res = ::strcmp(lhs->m_name.c_str(),rhs->m_name.c_str()); 
     if (res == -1) 
      return true; 
     else 
      return false; 
    } 
    }; 

    class SortByDept { 
    public: 
    bool operator()(const Employee* lhs, const Employee* rhs) { 
     int res = ::strcmp(lhs->m_dept.c_str(),rhs->m_dept.c_str()); 
     if (res == -1) 
      return true; 
     else 
      return false; 
    } 
    }; 

    class SortById { 
    public: 
    bool operator()(const Employee* lhs, const Employee* rhs) { 
     int res = ::strcmp(lhs->m_id.c_str(),rhs->m_id.c_str()); 
     if (res == -1) 
      return true; 
     else 
      return false; 
    } 
    }; 

    std::ostream& operator<<(std::ostream& o, const Employee* rhs) { 
    o << rhs->m_name << "," << rhs->m_dept << "," << rhs->m_id; 
    return o; 
    } 

    int main(int argc, char* argv[]) { 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 

      std::vector<int>::iterator ifind = std::find(v.begin(), v.end(), -4); 
      if (ifind != v.end()) 
       std::cout << "Item found " << *ifind << std::endl; 
      else 
       std::cout << "Item was not found" << std::endl; 

      ifind = std::find_if(v.begin(), v.end(), Criteria<int>(4)); 
      if (ifind != v.end()) 
       std::cout << "Item found " << *ifind << std::endl; 
      else 
       std::cout << "Item was not found" << std::endl; 
    } 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 
      v[4] = 5; // create an adjacent pair 

      std::vector<int>::iterator ifind = std::adjacent_find(v.begin(), v.end()); 
      if (ifind != v.end()) 
       std::cout << "Items found " << *ifind << " " << *(ifind+1) << std::endl; 
      else 
       std::cout << "Items were not found" << std::endl; 

      v[4] = 4; // create an adjacent pair 
      ifind = std::adjacent_find(v.begin(), v.end(), BinaryCriteria<int>()); 
      if (ifind != v.end()) 
       std::cout << "Items found " << *ifind << " " << *(ifind+1) << std::endl; 
      else 
       std::cout << "Items were not found" << std::endl; 
    } 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 
      v[3] = 3; 
      v[7] = 3; // some duplicates 

      std::cout << "The item " << 2 << " appears " 
         << std::count(v.begin(), v.end(), 2) 
         << " times" << std::endl; 

      std::cout << "The item " << 3 << " appears " 
         << std::count_if(v.begin(), v.end(), Criteria<int>(3)) 
         << " times" << std::endl; 

    } 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 

      std::for_each(v.begin(), v.end(), Print<int>(" ")); 
      std::cout << std::endl; 
      std::for_each(v.begin(), v.end(), print); 
      std::cout << std::endl; 
    } 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 
      int tab[3] = { 0, 1, 2 }; 

      bool res = std::equal(tab, tab+3, v.begin()); 
      if (res) 
       std::cout << "Ranges are equal" << std::endl; 
      else 
       std::cout << "Ranges are not equal" << std::endl; 


      v[0] = 2 * tab[0]; 
      v[1] = 2 * tab[1]; 
      v[2] = 2 * tab[2]; 
      res = std::equal(tab, tab+3, v.begin(), Equal<int>()); 
      if (res) 
       std::cout << "Ranges are equal" << std::endl; 
      else 
       std::cout << "Ranges are not equal" << std::endl; 

      res = std::equal(tab, tab+3, v.begin()); 
      if (res) 
       std::cout << "Ranges are equal" << std::endl; 
      else 
       std::cout << "Ranges are not equal" << std::endl; 

      res = std::equal(tab, tab+3, &v[5], Equal<int>()); 
      if (res) 
       std::cout << "Ranges are equal" << std::endl; 
      else 
       std::cout << "Ranges are not equal" << std::endl; 

    } 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 
      int tab[6] = { 0, 1, 2, 4, 5, 6 }; 

      std::pair<int*, std::vector<int>::iterator> res = 
       std::mismatch(tab, tab+6, v.begin()); 
      if (res.first != (tab+6)) { 
       std::cout << "mismatch located " 
        << "item in first range " << *res.first << 
         " item in second range " << *res.second << std::endl; 
      } else 
       std::cout << "No mismatch found within the range" << std::endl; 

      v[0] = 2 * tab[0]; 
      v[1] = 2 * tab[1]; 
      v[2] = 2 * tab[2]; 
      tab[3] = -99; 
      res = std::mismatch(tab, tab+6, v.begin(), Equal<int>()); 
      if (res.first != (tab+6)) { 
       std::cout << "mismatch located " 
        << "item in first range " << *res.first << 
         " item in second range " << *res.second << std::endl; 
      } else 
       std::cout << "No mismatch found within the range" << std::endl; 

    } 

    if (false) { 

      std::vector<int> v; 
      for (int i=0; i<10; i++) 
       v.push_back(i); 
      int tab[3] = { 4, 5, 6 }; 

      std::vector<int>::iterator res = std::search(v.begin(), v.end(), tab, tab+3); 
      if (res != v.end()) 
       std::cout << "Subsequence located - first item = " << *res << std::endl; 
      else 
       std::cout << "Subsequence not located" << std::endl; 

      tab[0] = 2 * v[5]; 
      tab[1] = 2 * v[6]; 
      tab[2] = 2 * v[7]; 
      // predicate function version 
      res = std::search(v.begin(), v.end(), tab, tab+3, Equal<int>()); 
      if (res != v.end()) 
       std::cout << "Subsequence located - first item = " << *res << std::endl; 
      else 
       std::cout << "Subsequence not located" << std::endl; 

    } 

    if (false) { 

      std::vector<int> v(10); 
      int tab[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; 
      std::copy(tab,tab+10,v.begin()); 
      std::for_each(v.begin(),v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

      std::copy_backward(tab,tab+10,v.end()); 
      std::for_each(v.begin(),v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

      std::vector<char> v2; 
      const char* str = "ABCDEFGHIJ"; 
      std::copy(str,str+10,std::back_inserter(v2)); 

      std::cout << "Before shift left" << std::endl; 
      std::for_each(v2.begin(),v2.end(),Print<char>("")); 
      std::cout << std::endl; 

      std::copy(v2.begin()+1, v2.end(), v2.begin()); 

      std::cout << "After shift left" << std::endl; 
      std::for_each(v2.begin(),v2.end(),Print<char>("")); 
      std::cout << std::endl; 

      std::copy(str,str+10,v2.begin()); 

      std::cout << "Before shift right" << std::endl; 
      std::for_each(v2.begin(),v2.end(),Print<char>("")); 
      std::cout << std::endl; 

      std::copy_backward(v2.begin(), v2.end()-1, v2.end()); 

      std::cout << "After shift right" << std::endl; 
      std::for_each(v2.begin(),v2.end(),Print<char>("")); 
      std::cout << std::endl; 

    } 

    if (false) { 

      std::vector<int> v(10); 
      std::fill(v.begin(),v.end(),-5); 
      std::for_each(v.begin(),v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

      std::fill_n(v.begin(),10,-5); 
      std::for_each(v.begin(),v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

    } 

    if (false) { 

      std::vector<std::string> v(10); 

      std::generate(v.begin(),v.end(),File<std::string>("c:/temp/items.txt")); 
      std::for_each(v.begin(),v.end(),Print<std::string>("\n")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      std::vector<int> v; 
      for(int i=0; i<10; i++) 
       v.push_back(i); 
      std::vector<int>::iterator res = 
       std::partition(v.begin(),v.end(),Odd<int>()); 

      // display each range 
      std::cout << "first range "; 
      std::for_each(v.begin(),res,Print<int>(" ")); 
      std::cout << std::endl; 

      std::cout << "second range "; 
      std::for_each(res,v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

      // repeat for stable_partition 
      for(int i=0; i<10; i++) 
       v[i]=i; 
      res = std::stable_partition(v.begin(),v.end(),Odd<int>()); 

      // display each range 
      std::cout << "first range "; 
      std::for_each(v.begin(),res,Print<int>(" ")); 
      std::cout << std::endl; 

      std::cout << "second range "; 
      std::for_each(res,v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

      // repeat using STL components 
      for(int i=0; i<10; i++) 
       v[i]=i; 
      res = std::partition(v.begin(),v.end(), 
       std::bind2nd( std::greater<int>(), 2)); 

      // display each range 
      std::cout << "first range "; 
      std::for_each(v.begin(),res,Print<int>(" ")); 
      std::cout << std::endl; 

      std::cout << "second range "; 
      std::for_each(res,v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

      // repeat using STL components 
      for(int i=0; i<10; i++) 
       v[i]=i; 
      res = std::partition(v.begin(),v.end(), 
       std::bind1st( std::greater<int>(), 2)); 

      // display each range 
      std::cout << "first range "; 
      std::for_each(v.begin(),res,Print<int>(" ")); 
      std::cout << std::endl; 

      std::cout << "second range "; 
      std::for_each(res,v.end(),Print<int>(" ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      // random_shuffle randomly rearranges the items in a range. 
      std::vector<int> v; 
      for(int i=0; i<100; i++) 
       v.push_back(i); 
      std::random_shuffle(v.begin(),v.end()); 
      std::copy(v.begin(),v.end(), 
       std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      int buf[10] = { 2, 5, 3, 5, 4, 5, 6, 6, 5, 5 }; 

      int* res = std::remove(buf, buf+10, 5); 

      std::cout << "items to keep "; 
      std::copy(buf, res, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

      std::cout << "items to remove "; 
      std::copy(res, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

      // test with vector 
      std::vector<int> v; 
      for(int i=0; i<10; i++) 
       v.push_back(i); 

      std::cout << "size of v before call to remove " << v.size() << std::endl; 
      std::vector<int>::iterator p = std::remove(v.begin(), v.end(), 5); 
      std::cout << "size of v after call to remove " << v.size() << std::endl; 

      v.erase(p); 
      std::cout << "size of v after erase " << v.size() << std::endl; 
      std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      int buf[10] = { 2, 5, 3, 5, 4, 5, 6, 6, 5, 5 }; 
      std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 
      std::replace(buf, buf+10, 5, 0); 
      std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      // The reverse algorithm reverses the order of the items in a given range. 
      int buf[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

      std::cout << "Before reverse "; 
      std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

      std::reverse(buf, buf+10); 

      std::cout << "After reverse "; 
      std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      int buf[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

      std::cout << "Before rotate "; 
      std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

      std::rotate(buf, buf+4, buf+10); 

      std::cout << "After rotate "; 
      std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      // The swap function swaps to values 
      std::string first="Hello"; 
      std::string second="World"; 
      std::cout << first << " " << second << std::endl; 
      std::swap(first,second); 
      std::cout << first << " " << second << std::endl; 

      // The swap_ranges function swaps ranges. 
      std::list<std::string> ll; 
      ll.push_front("Homer"); 
      ll.push_front("Marge"); 
      ll.push_front("Bart"); 

      std::vector<std::string> vec; 
      vec.push_back("Moe"); 
      vec.push_back("Wiggum"); 
      vec.push_back("Burns"); 

      std::swap_ranges(ll.begin(),ll.end(),vec.begin()); 

      std::cout << "List values "; 
      std::for_each(ll.begin(),ll.end(),Print<std::string>(" ")); 
      std::cout << std::endl; 

      std::cout << "Vector values "; 
      std::for_each(vec.begin(),vec.end(),Print<std::string>(" ")); 
      std::cout << std::endl; 

    } 

    if (false) { 
      std::vector<std::string> v(3); 
      std::list<std::string> ll; 
      ll.push_front("Homer"); 
      ll.push_front("Marge"); 
      ll.push_front("Bart"); 
      std::transform(ll.begin(),ll.end(),v.begin(),Upper<std::string>()); 
      std::cout << "Uppercase names "; 
      std::for_each(v.begin(),v.end(),Print<std::string>(" ")); 
      std::cout << std::endl; 

      std::list<std::string> ll2; 
      ll2.push_front("Simpson"); 
      ll2.push_front("Simpson"); 
      ll2.push_front("Simpson"); 
      std::transform(ll.begin(),ll.end(),ll2.begin(),v.begin(),Concat<std::string>(" ")); 
      std::cout << "Concat'ed names "; 
      std::for_each(v.begin(),v.end(),Print<std::string>("\n")); 
      std::cout << std::endl; 

      int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
      int b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
      int c[10]; 
      std::transform(a,a+10,b,c,std::plus<int>()); 
      std::for_each(c,c+10,Print<int>(" ")); 
      std::cout << std::endl; 

      std::transform(a,a+10,c,std::bind2nd(std::plus<int>(),10)); 
      std::for_each(c,c+10,Print<int>(" ")); 
      std::cout << std::endl; 


    } 

    if (false) { 
      int tab[10] = { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7 }; 
      int* res = std::unique(tab, tab+10); 

      std::cout << "Unique items "; 
      std::for_each(tab,res,Print<int>(" ")); 
      std::cout << std::endl; 

      // Binary predicate function version. 
      int tab2[10] = { 1, 2, 20, 3, 4, 40, 5, 6, 60, 7 }; 
      res = std::unique(tab2, tab2+10, TheSame<int>()); 

      std::cout << "Unique items "; 
      std::for_each(tab2,res,Print<int>(" ")); 
      std::cout << std::endl; 

      int tab3[10] = { 1, 2, 20, 3, 4, 40, 5, 6, 60, 7 }; 
      res = std::unique(tab3, tab3+10); 

      std::cout << "Unique items "; 
      std::for_each(tab3,res,Print<int>(" ")); 
      std::cout << std::endl; 

    } 

    if (false) { 

      int tab[10]; 

      // initialize 
      std::generate(tab,tab+10,Rand<int>()); 

      std::cout << "Unsorted sequence "; 
      std::for_each(tab,tab+10,Print<int>(" ")); 
      std::cout << std::endl; 

      std::sort(tab,tab+10,std::less<int>()); 

      std::cout << "Sorted ascending order sequence "; 
      std::for_each(tab,tab+10,Print<int>(" ")); 
      std::cout << std::endl; 

      // initialize 
      std::generate(tab,tab+10,Rand<int>()); 

      std::cout << "Unsorted sequence "; 
      std::for_each(tab,tab+10,Print<int>(" ")); 
      std::cout << std::endl; 

      std::sort(tab,tab+10,std::greater<int>()); 

      std::cout << "Sorted descending order sequence "; 
      std::for_each(tab,tab+10,Print<int>(" ")); 
      std::cout << std::endl; 

      // customize the sort criteria 
      std::vector<Employee*> v; 
      v.push_back(new Employee("EEE", "Dept AAA", "Id100")); 
      v.push_back(new Employee("CCC", "Dept BBB", "Id500")); 
      v.push_back(new Employee("BBB", "Dept DDD", "Id300")); 
      v.push_back(new Employee("FFF", "Dept CCC", "Id600")); 
      v.push_back(new Employee("AAA", "Dept CCC", "Id200")); 
      v.push_back(new Employee("DDD", "Dept AAA", "Id000")); 

      std::cout << "Unsorted employees " << std::endl; 
      std::for_each(v.begin(),v.end(),Print<Employee*>("\n")); 
      std::cout << std::endl; 

      // sort by name 
      std::sort(v.begin(),v.end(),SortByName()); 

      std::cout << "Sorted employees by name " << std::endl; 
      std::for_each(v.begin(),v.end(),Print<Employee*>("\n")); 
      std::cout << std::endl; 

      // sort by id 
      std::sort(v.begin(),v.end(),SortById()); 

      std::cout << "Sorted employees by id " << std::endl; 
      std::for_each(v.begin(),v.end(),Print<Employee*>("\n")); 
      std::cout << std::endl; 

      // sort by dept 
      std::sort(v.begin(),v.end(),SortByDept()); 

      std::cout << "Sorted employees by dept " << std::endl; 
      std::for_each(v.begin(),v.end(),Print<Employee*>("\n")); 
      std::cout << std::endl; 


      // sort by name, the by dept 
      std::sort(v.begin(),v.end(),SortByName()); 
      std::stable_sort(v.begin(),v.end(),SortByDept()); 
      std::cout << "Sorted employees by name then stable sorted by dept " << std::endl; 
      std::for_each(v.begin(),v.end(),Print<Employee*>("\n")); 
      std::cout << std::endl; 


      // initialize 
      std::generate(tab,tab+10,Rand<int>()); 

      std::cout << "Unsorted sequence " << std::endl; 
      std::for_each(tab,tab+10,Print<int>(" ")); 
      std::cout << std::endl; 

      std::partial_sort(tab,tab+3,tab+10,std::greater<int>()); 

      std::cout << "Partially sorted (top 3) in descending order sequence " << std::endl; 
      std::for_each(tab,tab+10,Print<int>(" ")); 
      std::cout << std::endl; 

      double salaries[10] = { 22000.0, 28500.00, 17500.00, 22000.0, 54000.00, 
       77500.00, 18400.00, 100000.00, 23000.0, 50000.0 }; 
      std::nth_element(salaries, salaries+2, salaries+10, std::less<double>()); 
      std::cout << "The 3rd top salary is " << salaries[2] << std::endl; 
      std::cout << std::endl; 

      } 

    if (false) { 

      std::vector<int> keys; 
      for (int i=0; i<100; i++) 
       keys.push_back(i); 
      std::sort(keys.begin(), keys.end(), std::less<int>()); 

      // is item 55 in the sequence? 
      bool isThere = std::binary_search(keys.begin(), keys.end(), int(55)); 
      if (isThere) 
       std::cout << "The key 55 is in the sequence" << std::endl; 
      else 
       std::cout << "The key 55 is not in the sequence" << std::endl; 

      // is item 555 in the sequence? 
      isThere = std::binary_search(keys.begin(), keys.end(), int(555)); 
      if (isThere) 
       std::cout << "The key 555 is in the sequence" << std::endl; 
      else 
       std::cout << "The key 555 is not in the sequence" << std::endl; 

      } 

    if (false) { 

      std::list<float> ll; 
      ll.push_front(70.0f); 
      ll.push_front(60.0f); 
      ll.push_front(50.0f); 
      ll.push_front(30.0f); 
      ll.push_front(20.0f); 
      ll.push_front(10.0f); 
      std::list<float>::iterator iter = 
       std::lower_bound(ll.begin(), ll.end(), 40.0f); 
      ll.insert(iter, 40.0f); 
      std::for_each(ll.begin(), ll.end(), Print<float>(" ")); 
      std::cout << std::endl; 

      std::list<float> ll2; 
      ll2.push_front(70.0f); 
      ll2.push_front(60.0f); 
      ll2.push_front(50.0f); 
      ll2.push_front(30.0f); 
      ll2.push_front(20.0f); 
      ll2.push_front(10.0f); 
      std::list<float>::iterator iter2 = 
       std::upper_bound(ll2.begin(), ll2.end(), 40.0f); 
      ll2.insert(iter2, 40.0f); 
      std::for_each(ll2.begin(), ll2.end(), Print<float>(" ")); 
      std::cout << std::endl; 

      std::list<float> ll3; 
      ll3.push_front(70.0f); 
      ll3.push_front(60.0f); 
      ll3.push_front(50.0f); 
      ll3.push_front(30.0f); 
      ll3.push_front(20.0f); 
      ll3.push_front(10.0f); 
      std::pair< std::list<float>::iterator, std::list<float>::iterator > res = 
       std::equal_range(ll3.begin(), ll3.end(), 40.0f); 
      std::cout << *res.first << " " << *res.second << std::endl; 
      std::cout << std::endl; 

      } 


    if (false) { 
     int tab1[5] = { 10, 20, 30, 40, 50 }; 
     int tab2[5] = { 5, 15, 25, 35, 45 }; 
     int tab3[10]; 
     std::merge(tab1,tab1+5,tab2,tab2+5,tab3); 
     std::cout << "The merged list "; 
     std::for_each(tab3,tab3+10,Print<int>(" ")); 
     std::cout << std::endl; 

     int tab4[10] = { 10, 20, 30, 40, 50, 5, 15, 25, 35, 45 }; 
     std::inplace_merge(tab4,tab4+5,tab4+10); 
     std::cout << "The in-place merged list "; 
     std::for_each(tab4,tab4+10,Print<int>(" ")); 
     std::cout << std::endl; 

    } 

    if (false) { 
     // The set algorithms allows set operations on sorted sequences. 
     int tab1[5] = { 10, 20, 30, 40, 50 }; 
     int tab2[5] = { 10, 25, 30, 45, 50 }; 
     int tab3[10]; 

     // union 
     int* p = std::set_union(tab1,tab1+5,tab2,tab2+5,tab3); 
     std::cout << "Union "; 
     std::for_each(tab3,p,Print<int>(" ")); 
     std::cout << std::endl; 

     // intersection 
     p = std::set_intersection(tab1,tab1+5,tab2,tab2+5,tab3); 
     std::cout << "Intersection "; 
     std::for_each(tab3,p,Print<int>(" ")); 
     std::cout << std::endl; 

     // difference 
     p = std::set_difference(tab1,tab1+5,tab2,tab2+5,tab3); 
     std::cout << "Difference "; 
     std::for_each(tab3,p,Print<int>(" ")); 
     std::cout << std::endl; 

     // symmetric difference 
     p = std::set_symmetric_difference(tab1,tab1+5,tab2,tab2+5,tab3); 
     std::cout << "Symmetric difference "; 
     std::for_each(tab3,p,Print<int>(" ")); 
     std::cout << std::endl; 

     int tab4[3] = { 20, 30, 40 }; 
     bool res = std::includes(tab1,tab1+5,tab4,tab4+3); 
     if (res) 
      std::cout << "Located the sequence in tab4 within tab1" << std::endl; 
     else 
      std::cout << "The sequence in tab4 is not located within tab1" << std::endl; 

    } 

    if (true) { 

     int tab[10] = { 2, 4, 6, 1, 3, 5, 8, 7, 9, 100 }; 
     int* p = std::min_element(tab,tab+10); 
     std::cout << "Min value = " << *p << std::endl; 

     p = std::max_element(tab,tab+10); 
     std::cout << "Max value = " << *p << std::endl; 

    } 



    return 0; 
    } 
+4

代码太多。不是[SSCCE](http://sscce.org/)。 – Lion 2012-07-08 04:59:26

+0

我明白这是太多的代码,但只是将代码粘贴到IDE中,并且应该看到错误。这是我之后的构建错误,而不是代码。谢谢。 – 2012-07-08 05:02:43

+3

其实,你应该只发布给出错误的一行,以及一些你认为相关的信息。这是很多代码。 – 2012-07-08 05:04:11

回答

5

你的问题是你正在创建一个File对象的实例并将它传递给std::generate(在第405行左右)。 A File包含std::ifstream的一个实例,并且函子按值传递给std::generate,这意味着它接收到原始对象的副本 - 但由于此对象包含无法复制的std::ifstream,因此无法工作。

至于如何找到它:首先使用#if 0来禁用所有if (false)部分,并且您会发现它编译并运行得很好。

从那里,使用平分:通过将#if 0下移到(大约)该大块的中间(以及在if (false)行之一之前),启用其中的一半。看看它是否仍然编译。如果是这样,将#if 0下移至该部分下方约3/4的。如果不是这样,将其移动到通过该组块的方式(并且每次,确保它在if (false)之前)。通过几次迭代,您可以将其缩小到单个`if (false)块(在这种情况下,只有5行)。当它很小时,查看问题代码可能是最简单的。

+0

是的,我得到了同样的结果。有没有办法通过引用传递给std :: generate,如果有的话,你会告诉我如何做到这一点。谢谢。 – 2012-07-08 05:44:38

+0

@ZzzZz:你不能让'std :: generate'通过引用接受一个参数。显而易见的做法是防止“File”被完全复制。如果你不能改变代码,你可能需要考虑一个TR1/C++ 11'reference_wrapper'。我没有尝试过,但怀疑至少有一个让代码编译的好机会。 – 2012-07-08 05:49:03

9

您的文章不包括问题。我会假设你会问“这个错误信息是什么意思?”

的回答问题是:std::ifstream不可复制。

+1

好的。我接受,但为什么这个代码可以在VC++ 6.0下正确构建?谢谢。 – 2012-07-08 05:09:49

+1

该代码不正确。也许它可以在VC++ 6.0下构建,但根据定义,它不能在VC++ 6.0下正确构建。 – 2012-07-08 05:12:40

+0

@ZzzZz:标准说class_basic_ios应该有私有的,未定义的拷贝ctor和赋值运算符,使得类不可复制(并且'class ifstream <>'间接继承'base_ios',使ifstream'也是不可复制的) 。 VC++ 6不这样做。 – 2012-07-08 05:49:00

1

错误来自线路407

std::generate(v.begin(),v.end(),File<std::string>("c:/temp/items.txt")); 

这是从创建您的文件对象(在程序前面定义)的所产生的到来。现在,如果我明白这一行正确地做了什么,那么您将使用罚款的每一行填充向量(因为()运算符不断从打开的文件通过getline()函数返回一行)。如果更换,如果阻止

if (false) { 

     std::vector<std::string> v(10); 

     std::generate(v.begin(),v.end(),File<std::string>("c:/temp/items.txt")); 
     std::for_each(v.begin(),v.end(),Print<std::string>("\n")); 
     std::cout << std::endl; 

} 

在该块

if (false) { 
     std::vector<std::string> v; 
     std::ifstream fi("c:/temp/items.txt"); 
     std::string line; 

     while (!fi.eof()) { 
      getline (fi, line); 
      v.push_back (line); 
     } 
     fi.close(); 

     std::for_each(v.begin(),v.end(),Print<std::string>("\n")); 
     std::cout << std::endl; 
} 

你的程序应该按预期运行,当你打开,如果(假)到IF(真)以下。

另外,您可能需要包含cstring,因为您引用了strcmp,哪个(至少对于g ++)需要cstring头文件。

+0

我的代码在getline(fi,line)失败,请指出getline的完整限定符,因为它不在std :: getline中,而不在fstream :: getline中。谢谢。 – 2012-07-08 05:52:02

+0

这是istream中的getline,签名为:'istream&getline(istream&is,string&str)'您可以阅读它,并在此处查看示例:http://www.cplusplus.com/reference/string/ getline/ – Alex 2012-07-08 06:02:40

+0

我试过包括#include ,它编译了,但是我得到了错误LNK2019:无法解析的外部符号_wWinMain @ 16在函数___tmainCRTStartup错误中引用,任何想法为什么?谢谢。 – 2012-07-08 06:04:34