![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Location: Omaha, NE
Posts: 8
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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 (word.length() > 30)
word.resize(30); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|