我有以下类使用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传递给类的构造函数?
答案 0 :(得分:4)
class OSxFS
结尾处缺少分号。此外,您使用不正确的语法来调用path
的构造函数。尝试:
OSxFS(string _folderPath) :
mFolderPath(_folderPath)
{
}
mFolderPath(_folderPath);
构造函数正文中的 OSxFS
正在尝试将mFolderPath
作为函数调用。