开发者

How to handle C++ boost::filesystem functions that return string_type?

I'm trying to compile the following snippet of code:

#include <boost/filesystem/path.hpp>
//Other includes snipped for brevity

boost::filesystem::path inPath("C:\\PathThatDoesNotExist");
std::cou开发者_运维知识库t << "Folder \'" << inPath.external_directory_string() 
  << "\' does not exist.";

and I'm getting this error:

.\PathCheck.cpp(31) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const boost::filesystem3::path::string_type' (or there is no acceptable conversion)

My research so far tells me that this means the type is not known at compile time and that I might need a typedef? but I can't quite make that work in my head or in my code. The external_directory_string() function is deprecated, so I'm also concerned that could be a factor somehow (I'm handling it with #define BOOST_FILESYSTEM_DEPRECATED).

Can someone explain string_type and recommend how to turn it into a std::string or char*? I know there are other ways to get this information from a directory path, but I'd like to understand the issue anyway. :)

RESOLVED: I have v2 of the 1.44 boost libraries, but had "#define BOOST_FILESYSTEM_VERSION 3" in my header (a co-worker using another version had added it). If I comment this directive, my code compiles correctly. Looks like we need to synchronize our libraries.


With the mods below, your code compiles OK for me. This applies with or without the #define you mentioned.

boost::filesystem::path inpath("C:\\PathThatDoesNotExist");
std::cout << "Folder \'" << inpath.external_directory_string() 
    << "\' does not exist.";

What version of Boost, and what compiler are you using?

I am on Boost 1.44, Visual C++ Express 2010. external_directory_string appears to return std::string for me.

EDIT: However, when I explicitly set the Boost Filesystem version like this, I get your error:

#   define BOOST_FILESYSTEM_VERSION 3

The default version for me on VS 2010 is 2, so I would investigate why boost/filesystem/path.hpp is setting this to 3 on your build.

From the Boost docs (1.45):

This is Version 2 of the Filesystem library.

Version 3, a major revision with many new and improved features is also available, but breaks some existing code.

Sounds to me like if you get your build back to v2 you will be on more solid ground.


The boost::filesystem::path typedefs external_string_type to std::string (see here http://www.boost.org/doc/libs/1_43_0/boost/filesystem/path.hpp), which would suggest the problem lies elsewhere?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜