如何传递一个字符串类型的路径,以提高文件系统:::路径的构造?(How to pass a stri

2019-09-21 09:13发布

我有使用提高文件系统下面的类,但在编译时遇到的问题。

/// 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;
}

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

///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;
}

但是,我得到了错误信息,当我在Xcode编译它们用g ++:

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’

如果我实现一个单一的main.cpp类OSxFS的功能ShowStatus(),它的工作原理。 所以,我想这个问题是关于如何将字符串变量_folderPath传递给类的构造函数?

Answer 1:

这是因为丢失在结束一个分号class OSxFS 。 此外,您使用的是不正确的语法调用构造函数path 。 尝试:

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

mFolderPath(_folderPath); 在身体OSxFS构造函数试图调用mFolderPath的功能。



文章来源: How to pass a string type path to boost::filesystem:path's constructor?
标签: c++ boost g++