I don't understand this cryptic error message but I get 30 of
`'value_type' : is not a member of 'TextFileLineBuffer'`
when I compiled following code in VC++ 6 with //***
lines uncommented.
Of course, if I commented it out, it compiles fine.
I think I tried various attempts in vain for last two hours. Any tip would be appreciated.
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
//wrapper for a string line
struct TextLine
{
std::string m_sLineContent;
operator std::string const& () const { return m_sLineContent; }
friend std::istream& operator>>(std::istream& stream, TextLine& line)
{
return std::getline(stream, line.m_sLineContent);
}
};
//this is a version of fixed size of string queue for easy text file reading
class TextFileLineBuffer
{
public:
TextFileLineBuffer(size_t lc, const char* fileName)
: m_iLineCount(lc), m_sFileName(fileName)
{
std::ifstream file(fileName);
//*** std::copy(std::istream_iterator<TextLine>(file),
//*** std::istream_iterator<TextLine>(),
//*** std::back_inserter(*this));
}
void push_back(std::string const& line)
{
m_sBuffer.insert(m_sBuffer.end(),line);
if (m_sBuffer.size() > m_iLineCount)
{
m_sBuffer.erase(m_sBuffer.begin());
}
}
const char* c_str() const
{
std::string returnValue("");
for (const_iterator it = begin(); it != end(); ++it)
{
returnValue = returnValue + *it;
}
return returnValue.c_str();
}
typedef std::list<std::string> Container;
typedef Container::const_iterator const_iterator;
typedef Container::const_reference const_reference;
const_iterator begin() const { return m_sBuffer.begin(); }
const_iterator end() const { return m_sBuffer.end();}
private:
size_t m_iLineCount;
std::list<std::string> m_sBuffer;
std::string m_sFileName;
};
According to the standard (24.5.2.1 [back.insert.iterator]), back_insert_iterator
requires that your Container
type contain a value_type
typedef, which should name the base type of the (const reference or rvalue reference) argument to push_back
:
class TextFileLineBuffer
{
public:
// ...
typedef std::string value_type;
For compatibility with C++98, you should also define const_reference
, per std::back_inserter needs const_reference on older GCC. Why?:
typedef const std::string &const_reference;
I found my way here because I was trying to do this:
std::vector<Type_A, Type_B> someVec;
When I wanted to do this:
std::vector<std::pair<Type_A, Type_B>> someVec;
If your case is similar, keep in mind that the second templated type for a stl container specifies the memory allocator--which has particular requirements. One of which is that it defines value_type
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With