2012-07-08 96 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