Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 18th, 2005, 9:02 PM   #1
wingz198
Newbie
 
wingz198's Avatar
 
Join Date: Sep 2005
Location: Omaha, NE
Posts: 8
Rep Power: 0 wingz198 is on a distinguished road
String I/O and Vector Insert errors

I'm trying to read in a file of words that are all on seperate lines and insert them into a vector. I need to read the words in as strings and I'm getting errors when I try to use getline() and push_back. I was sure that at least the vector would work as every example I've seen works like that.

    vector<string> words;
    string word;

    while (!inputStream.eof())
    {
        inputStream.getline(word, 30);
        words.push_back(word);
    }

And I'm getting these errors:

main.cpp:28: no matching function for call to `std::basic_ifstream<char,
   std::char_traits<char> >::getline(std::string&, int)'
/usr/include/c++/3.2.2/bits/istream.tcc:664: candidates are:
   std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
   _Traits>::getline(_CharT*, int, _CharT) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.2.2/istream:176:                 std::basic_istream<_CharT,
   _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, int) [with
   _CharT = char, _Traits = std::char_traits<char>]

main.cpp:29: no matching function for call to `std::vector<char*,
   std::allocator<char*> >::push_back(std::string&)'
/usr/include/c++/3.2.2/bits/stl_vector.h:492: candidates are: void
   std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = char*, _Alloc =
   std::allocator<char*>]
make: *** [main.o] Error 1
wingz198 is offline   Reply With Quote
Old Oct 18th, 2005, 9:47 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
The std::basic_istream<_CharT, _Traits> class that you are using does not support std::strings as parameters to the getline function. Use the std:string getline function instead
    vector<string> words;
    string word;

    while (!inputStream.eof())
    {
        getline(inputStream, word);
        words.push_back(word);
    }
If you really want to limit the size to 30 characters, you coudl add the following lines after the getline
  if (word.length() > 30)
    word.resize(30);
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:30 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC