Problems with std::copy()

This is a discussion on Problems with std::copy() within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use istream_iterators to read from a file and put words into a set<word>. Word is a class ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question Problems with std::copy()

    I'm trying to use istream_iterators to read from a file and put words into a set<word>. Word is a class that I've defined that takes a string and removes all non-letters such as punctuation. When I compile I get a really disgusting looking error. Any help? I'm using the newest DJGPP if that helps at all.
    Code:
    set<word> Words;
    	set<word>::iterator it=Words.begin();
    		
    	std::copy(std::istream_iterator<string>(fin),std::istream_iterator<string>(),std::inserter(Words,it));
    Error:
    c:/djgpp/lang/cxx/3.32/bits/stl_function.h: In member function `bool
    std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = word]':
    c:/djgpp/lang/cxx/3.32/bits/stl_tree.h:1066: instantiated from `std::_Rb_tree_
    iterator<_Val, _Val&, _Val*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _A
    lloc>::insert_unique(std::_Rb_tree_iterator<_Val, _Val&, _Val*>, const _Val&) [w
    ith _Key = word, _Val = word, _KeyOfValue = std::_Identity<word>, _Compare = std
    ::less<word>, _Alloc = std::allocator<word>]'
    c:/djgpp/lang/cxx/3.32/bits/stl_set.h:160: instantiated from `typename std::_R
    b_tree<_Key, _Key, std::_Identity<_Key>, _Compare, _Alloc>::const_iterator std::
    set<_Key, _Compare, _Alloc>::insert(typename std::_Rb_tree<_Key, _Key, std::_Ide
    ntity<_Key>, _Compare, _Alloc>::const_iterator, const _Key&) [with _Key = word,
    _Compare = std::less<word>, _Alloc = std::allocator<word>]'
    c:/djgpp/lang/cxx/3.32/bits/stl_iterator.h:524: instantiated from `std::insert
    _iterator<_Container>& std::insert_iterator<_Container>::operator=(typena me _Con
    tainer::const_reference) [with _Container = std::set<word, std::less<word>, std:
    :allocator<word> >]'
    c:/djgpp/lang/cxx/3.32/bits/stlalgobase.h:228: instantiated from `_OutputIter
    std::__copy(_InputIter, _InputIter, _OutputIter, std::input_iterator_tag) [with
    _InputIter = std::istream_iterator<std::string, char, std::char_traits<char>, pt
    rdiff_t>, _OutputIter = std::insert_iterator<std::set<word, std::less<word>, std
    ::allocator<word> > >]'
    c:/djgpp/lang/cxx/3.32/bits/stlalgobase.h:260: instantiated from `_OutputIter
    std::__copy_aux2(_InputIter, _InputIter, _OutputIter, __false_type) [with _Input
    Iter = std::istream_iterator<std::string, char, std::char_traits<char>, ptrdiff_
    t>, _OutputIter = std::insert_iterator<std::set<word, std::less<word>, std::allo
    cator<word> > >]'
    c:/djgpp/lang/cxx/3.32/bits/stlalgobase.h:303: instantiated from `_OutputIter
    std::__copy_ni2(_InputIter, _InputIter, _OutputIter, __false_type) [with _InputI
    ter = std::istream_iterator<std::string, char, std::char_traits<char>, ptrdiff_t
    >, _OutputIter = std::insert_iterator<std::set<word, std::less<word>, std::alloc
    ator<word> > >]'
    c:/djgpp/lang/cxx/3.32/bits/stlalgobase.h:323: instantiated from `_OutputIter
    std::__copy_ni1(_InputIter, _InputIter, _OutputIter, __false_type) [with _InputI
    ter = std::istream_iterator<std::string, char, std::char_traits<char>, ptrdiff_t
    >, _OutputIter = std::insert_iterator<std::set<word, std::less<word>, std::alloc
    ator<word> > >]'
    c:/djgpp/lang/cxx/3.32/bits/stlalgobase.h:349: instantiated from `_OutputIter
    std::copy(_InputIter, _InputIter, _OutputIter) [with _InputIter = std::istream_i
    terator<std::string, char, std::char_traits<char>, ptrdiff_t>, _OutputIter = std
    ::insert_iterator<std::set<word, std::less<word>, std::allocator<word> > >]'
    biblio.cpp:30: instantiated from here
    c:/djgpp/lang/cxx/3.32/bits/stl_function.h:197: error: passing `const word' as
    `this' argument of `bool word::operator<(word)' discards qualifiers

  2. #2
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Oops, I forgot to post word.h. Here's all my code:

    biblio.cpp
    Code:
    #include "word.h"
    #include <fstream>
    #include <iostream>
    #include <set>
    #include<iterator> /*istream_iterator definition*/
    #include<algorithm>
    using std::cout;
    using std::set;
    
    int main()
    {
    	std::ifstream fin;
    	set<word> Words;
    	
    	fin.open("Assignment5input.txt");
    	
    	if(fin.fail())
    	{
    		cout<<"Error opening Assignment5input.txt";
    		return -1;
    	}
    	
    	std::copy(std::istream_iterator<string>(fin),std::istream_iterator<string>(),std::inserter(Words,Words.begin()));
    	
    	return 0;
    }
    word.h
    Code:
    #include<string>
    #include<cctype>
    using std::string;
    
    class word
    {
    	public:
    		word(string str1);
    		bool operator<(word two);
    	private:
    		string str;
    };
    
    word::word(string str1)
    {
    	int spot=0;
    	char wrd[50];
    	for(unsigned i=0;i<str1.length();i++)
    	{
    		if(isalpha(str1.c_str()[i]))
    		{
    			wrd[spot]=tolower(str1.c_str()[i]);
    			spot++;
    		}
    	}
    	str=(string)wrd;
    }
    
    bool word::operator<(word two)
    {
    	int size;
    	
    	if(str.length()<two.str.length())
    		size=str.length();
    	else
    		size=str.length();
    	
    	for(int i=0;i<size;i++)
    		if(str.c_str()[i]<two.str.c_str()[i])
    			return true;
    	return false;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >passing `const word' as`this' argument of `bool word::operator<(word)' discards qualifiers
    Make these two changes:
    Code:
    bool operator<(word two) const;
    Code:
    bool word::operator<(word two) const
    My best code is written with the delete key.

  4. #4
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Dude! Thank you VERY much. I really appreciate that! Is there an FAQ or something I could read that would teach me why I need that const? Again, I can't thank you enough.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there an FAQ or something I could read that would teach me why I need that const?
    If your object is const then any member functions it uses must be const qualified as well. A good book on C++ should explain const correctness.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21