如何将一个字符串类型路径传递给boost :: filesystem:path的构造函数? [英] How to pass a string type path to boost::filesystem:path's constructor?

查看:330
本文介绍了如何将一个字符串类型路径传递给boost :: filesystem:path的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用boost文件系统有以下类,但在编译时遇到了问题。

  /// tfs.h file :

#include< boost / filesystem.hpp>
#include< iostream>
#include< string>

使用命名空间boost;
使用namespace std;

class OSxFS
{
public:
OSxFS(string _folderPath)
{
mFolderPath(_folderPath);

$ b b字符串ShowStatus()
{
尝试
{
filesystem :: file_status folderStatus = filesystem :: status(mFolderPath);
cout<<文件夹状态:<<<<文件系统:: is_directory(folderStatus)<< endl;
}
catch(filesystem :: filesystem_error& e)
{
cerr<错误消息:<<< e.what()<<< ; ENDL;
}
}

private:
filesystem :: path mFolderPath;

$ / code>

在m.cpp文件中,我使用下面的代码来调用OSxFS class:

  /// m.cpp文件

#includetfs.h
#include< iostream>
#include< string>

使用namespace std;
使用命名空间boost;

int main()
{
string p =〜/ Desktop /;
OSxFS folderX(p);
folderX.ShowStatus();
cout<<谢谢!<< endl;
返回0;
}

然而,当我在xCode中用g ++编译它时,我收到了错误信息:

 在m.cpp包含的文件中:1:
tfs.h:在构造函数'OSxFS :: OSxFS (boost :: filesystem :: path)(std :: string&)'
m.cpp:'b
tfs.h:13:error:在全局范围内:
m.cpp:5:错误:在'使用'之前预期的非限定ID

如果我在一个main.cpp中实现OSxFS类的函数ShowStatus(),它就可以工作。所以,我想问题是如何将字符串变量_folderPath传递给类的构造函数?类OSxFS 的末尾。此外,您正在使用不正确的语法来调用 path 的构造函数。尝试:

pre code OSxFS(字符串_folderPath)
mFolderPath(_folderPath)
{
}

mFolderPath(_folderPath); OSxFS 构造函数尝试调用 mFolderPath 作为函数。


I have the following class using boost filesystem, but encountered the problem when compiling.

/// tfs.h file:

#include <boost/filesystem.hpp>
#include <iostream>
#include <string>

using namespace boost;
using namespace std;

class OSxFS
{
  public:
    OSxFS(string _folderPath)
    {
      mFolderPath(_folderPath);
    }

    string ShowStatus()
    {
      try
      {
        filesystem::file_status folderStatus = filesystem::status(mFolderPath);
        cout<<"Folder status: "<<filesystem::is_directory(folderStatus)<<endl;
      }
      catch(filesystem::filesystem_error &e)
      {
        cerr<<"Error! Message: "<<e.what()<<endl;
      }
    }

  private:
    filesystem::path mFolderPath;
}

In the m.cpp file, I use the following code to invoke the OSxFS class:

///m.cpp file 

#include "tfs.h"
#include <iostream>
#include <string>

using namespace std;
using namespace boost;

int main()
{  
  string p = "~/Desktop/";
  OSxFS folderX(p);
  folderX.ShowStatus();
  cout<<"Thank you!"<<endl;
  return 0;
}

However, I got the error message when I compile them by g++ in xCode:

In file included from m.cpp:1:
tfs.h: In constructor ‘OSxFS::OSxFS(std::string)’:
tfs.h:13: error: no match for call to ‘(boost::filesystem::path) (std::string&)’
m.cpp: At global scope:
m.cpp:5: error: expected unqualified-id before ‘using’

If I implement the function ShowStatus() of class OSxFS in a single main.cpp, it works. So, I guess the problem is about how to pass the string variable _folderPath to the class's constructor?

解决方案

You are missing a semicolon at the end of class OSxFS. Also, you are using the incorrect syntax to call the constructor of path. Try:

OSxFS(string _folderPath) :
    mFolderPath(_folderPath)
{ 
}

mFolderPath(_folderPath); in the body of the OSxFS constructor is attempting to call mFolderPath as a function.

这篇关于如何将一个字符串类型路径传递给boost :: filesystem:path的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆