Hello,
I came across a strange error and it's really been bugging me. Maybe
someone else has come across this and any insight would be
appreciated.
What I'm trying to accomplish is using boost::lexical_ cast to cast a
vector of strings to a string.
The compiler I'm using is MSVC++ 2005 Express Edition. The gc++
compiler does not have the same complaint, this is strictly a MSVC++
2005 issue. I don't think it matters, but I'm running on a WinXP 32
bit machine. I'm also using boost v1.34.0.
Here's the code.
#include <boost/lexical_cast.hp p>
#include <iostream>
#include <vector>
#include <string>
//--------------------------------------------------------------
template<class T>
std::ostream & operator<<(std: :ostream& s, const std::vector<T& d) {
typedef typename std::vector<T>: :const_iterator iter;
iter it;
for (it = d.begin() ; it != d.end() ; ++it) {
s << *it;
s << "\n";
}
return s;
}
//--------------------------------------------------------------
template<class T>
std::istream & operator>>(std: :istream& s, std::vector<T& d) {
while (!s.eof()) {
char buf[500];
s.getline(buf, sizeof(buf));
d.push_back(buf );
}
return s;
}
//--------------------------------------------------------------
int main (int argc, char ** argv) {
std::vector<std ::stringvecstr1 ;
vecstr1.push_ba ck("hi");
vecstr1.push_ba ck("there");
std::string str = boost::lexical_ cast<std::strin g>(vecstr1);
std::cout << str << std::endl;
return 0;
}
This compiles quite nicely and even spits out the correct output. Now
for the problem:
If I change the include #includes at the top to:
#include <boost/lexical_cast.hp p>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
#include <string>
I get the following compilation error (only the last few lines are
provided for clarity):
lexical_cast.hp p(150) : while compiling class template member function
'bool boost::detail:: lexical_stream< Target,Source>: :operator <<(const
Source &)'
with
[
Target=std::str ing,
Source=NewSourc e
]
lexical_cast.hp p(219) : see reference to class template instantiation
'boost::detail: :lexical_stream <Target,Source> ' being compiled
with
[
Target=std::str ing,
Source=NewSourc e
]
main.cpp(38) : see reference to function template instantiation
'Target boost::lexical_ cast<std::strin g,std::vector<_ Ty>>(const Source
&)' being compiled
with
[
Target=std::str ing,
_Ty=std::string ,
Source=std::vec tor<std::string >
]
lexical_cast.hp p(151) : error C2228: left of '.fail' must have class/
struct/union
Now, for a really stupid solution:
#include <boost/lexical_cast.hp p>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
#include <string>
//--------------------------------------------------------------
// inheriting from an STL container - asking for trouble!
// But it does solve the compilation problem. WHY????
class stringvector : public std::vector<std ::string{
};
//--------------------------------------------------------------
template<class T>
std::ostream & operator<<(std: :ostream& s, const std::vector<T& d) {
typedef typename std::vector<T>: :const_iterator iter;
iter it;
for (it = d.begin() ; it != d.end() ; ++it) {
s << *it;
s << "\n";
}
return s;
}
//--------------------------------------------------------------
template<class T>
std::istream & operator>>(std: :istream& s, std::vector<T& d) {
while (!s.eof()) {
char buf[500];
s.getline(buf, sizeof(buf));
d.push_back(buf );
}
return s;
}
//--------------------------------------------------------------
int main (int argc, char ** argv) {
stringvector vecstr1; // replaced std::vector<Twi th stringvector
vecstr1.push_ba ck("hi");
vecstr1.push_ba ck("there");
std::string str = boost::lexical_ cast<std::strin g>(vecstr1);
std::cout << str << std::endl;
return 0;
}
This code compiles, but inheriting from an STL container is not my
idea...of a good idea...
So what's going on here?
There are basically 2 questions I have:
1) why does the boost/shared_ptr.hpp inclusion effect a
boost::lexical_ cast<std::strin g, std::vectordecl aration?
2) How does inheriting from the std::vector class solve this problem?
Thanks in advance.
Hans Smit