如何将字符串类型路径传递给boost :: filesystem:path的构造函数?

时间:2012-08-20 03:13:41

标签: c++ boost g++

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

/// 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传递给类的构造函数?

1 个答案:

答案 0 :(得分:4)

class OSxFS结尾处缺少分号。此外,您使用不正确的语法来调用path的构造函数。尝试:

OSxFS(string _folderPath) :
    mFolderPath(_folderPath)
{ 
}
mFolderPath(_folderPath);构造函数正文中的

OSxFS正在尝试将mFolderPath作为函数调用。