2016-07-04 69 views
1

我有这些辅助功能:C++ libgen.h(MinGW的)失踪vc140 64项目 - 辅助功能

std::string 
pathname_directory(const std::string &pathname) 
{ 
    char buffer[pathname.size() + 1]; 
    memset(buffer, 0, sizeof(buffer)); 
    std::copy(pathname.begin(), pathname.end(), buffer); 
    return std::string(dirname(buffer)); 
} 

std::string 
pathname_sans_directory(const std::string &pathname) 
{ 
    char buffer[pathname.size() + 1]; 
    memset(buffer, 0, sizeof(buffer)); 
    std::copy(pathname.begin(), pathname.end(), buffer); 
    return std::string(basename(buffer)); 
} 

依靠libgen.h,一个MinGW的头。 1)为什么VS给出包含“char buffer [pathname.size()+ 1];”的行的错误“expression must have constant value”?

2)VS 2015中是否有预定义的函数来处理这个问题?

+0

什么是msvc2014? – rubenvb

+0

微软视觉工作室编译器2015年(由于某种原因称为2014年)? –

+0

你错了。 Microsoft Visual Studio 2015的编译器版本号是14.这里没有一年。 – rubenvb

回答

1

libgen.hPosix header, 所以这个问题与mingw没有特别的联系。

编译器抱怨:

char buffer[pathname.size() + 1]; 

因为标准C++(任何标准)禁止可变长度阵列(尽管 C99允许它们)。

你可能避免重写你的函数使用VLAS:

#include <string> 
#include <memory> 
#include <algorithm> 
#include <libgen.h> 

std::string 
pathname_directory(const std::string &pathname) 
{ 
    char const *cs = pathname.c_str(); 
    std::size_t cslen = pathname.size() + 1; 
    std::unique_ptr<char[]> pbuf(new char[cslen]); 
    std::copy(cs,cs + cslen,pbuf.get()); 
    return dirname(pbuf.get()); 
} 

std::string 
pathname_sans_directory(const std::string &pathname) 
{ 
    char const *cs = pathname.c_str(); 
    std::size_t cslen = pathname.size() + 1; 
    std::unique_ptr<char[]> pbuf(new char[cslen]); 
    std::copy(cs,cs + cslen,pbuf.get()); 
    return basename(pbuf.get()); 
} 

但是,你从dirname获取和basename也许 不值得得到它这样的繁琐程序(或实际上的方式帮助你 试图得到它)。

尤其如此,如果你想的pathname_directory(pathname) 什么是所有的pathname直到但不包括最后一个路径分隔符, 和你想要的pathname_sans_directory(pathname)什么是所有的pathname 最后一个路径分隔后。因为dirnamebasename 会通过返回“。”来让您大吃一惊。在你期望空字符串的情况下。 请参阅dirnamebasename 文档。

如果这是这样,最不麻烦会自己动手做的过程:

#include <string> 

std::string 
pathname_directory(const std::string &pathname) 
{ 
    std::size_t len = pathname.find_last_of("/\\"); 
    return len == std::string::npos ? "": pathname.substr(0,len); 
} 

std::string 
pathname_sans_directory(const std::string &pathname) 
{ 
    std::size_t len = pathname.find_last_of("/\\"); 
    return len == std::string::npos ? pathname : pathname.substr(len + 1); 
} 
+0

看起来不错,我稍后会执行它并查看结果。谢谢:) –

+0

这工作,但还有其他相关的构建问题,我已经开始了一个新的问题在这里http://stackoverflow.com/questions/38242467/csiro-face-analysis-sdk-linux-to-windows-conversion –