C++ 改变函数定义的命名空间

C++ 改变函数定义的命名空间,c++,namespaces,C++,Namespaces,我有一小段代码,它只生成一个函数来获取Windows或Linux平台的当前目录: #include <stdio.h> /* defines FILENAME_MAX */ #include <string> #ifdef WINDOWS #include <direct.h> #define GetCurrentDir _getcwd #else #include <unistd.h> #define GetCu

我有一小段代码,它只生成一个函数来获取Windows或Linux平台的当前目录:

#include <stdio.h>  /* defines FILENAME_MAX */
#include <string>
#ifdef WINDOWS
    #include <direct.h>
    #define GetCurrentDir _getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
 #endif


    std::string getcwd(){
    char mCurrentPath[FILENAME_MAX];
    GetCurrentDir(mCurrentPath, sizeof(mCurrentPath));
    return *(new std::string (mCurrentPath));

    }
但是这在
VSCode
中给出了一个错误,它说:

调用“getcwd”时没有匹配函数

我在这方面犯了什么错误?如果这不是我将函数放入名称空间的方式,那么我应该如何将它放入名称空间?

当计算
GetCurrentDir
宏(to
getcwd
)时,您的
fUtils::getcwd()
函数正在尝试调用自身,这就产生了一个函数,它不需要参数,只需要两个参数

要解决此问题,请在
GetCurrentDir
的定义中添加全局命名空间运算符(
),如下所示:

#ifdef窗口
#包括
#定义GetCurrentDir::\u getcwd
#否则
#包括
#定义GetCurrentDir::getcwd
#恩迪夫
然后,在函数体中,编译器可以清楚地看到,您并不是在寻找一个“递归”(且无效)调用。

您的
fUtils::getcwd()
函数试图在计算
GetCurrentDir
宏(to
getcwd
)时调用自己,这就产生了一个函数,它不需要参数,只需要两个参数

要解决此问题,请在
GetCurrentDir
的定义中添加全局命名空间运算符(
),如下所示:

#ifdef窗口
#包括
#定义GetCurrentDir::\u getcwd
#否则
#包括
#定义GetCurrentDir::getcwd
#恩迪夫

然后,在函数体中,编译器可以清楚地看到,您并不是在寻找“递归”(且无效)调用。

在这种上下文中使用宏会导致将来出现问题

只要用自己的API包装这些函数,所有问题都会得到解决

头文件:

#包括
徒劳的名字{
std::string getcwd();
}
然后您可以拥有特定于平台的cpp文件,Windows:

namepsace-fUtils{
std::string getcwd(){
char mCurrentPath[FILENAME_MAX];
::_getcwd(mCurrentPath,sizeof(mCurrentPath));
返回{mCurrentPath};
}
}
Mac版本是显而易见的

您还可以在单个cpp文件中使用此宏,如果您不知道如何进行更大的清理,则可以通过这种方式将它们包含在其中

旁注:
  • 还有
    boost::filesystem
    有这样的api,使用
    boost::filesystem::path
    非常方便
  • C++17引入了
    std::filesystem
    ,但它还没有得到很好的支持(例如MacOS)
  • 如果您将此功能封装在类中,您将为更好的测试(模拟)开辟道路

在这种情况下使用宏会引发未来的问题

只要用自己的API包装这些函数,所有问题都会得到解决

头文件:

#包括
徒劳的名字{
std::string getcwd();
}
然后您可以拥有特定于平台的cpp文件,Windows:

namepsace-fUtils{
std::string getcwd(){
char mCurrentPath[FILENAME_MAX];
::_getcwd(mCurrentPath,sizeof(mCurrentPath));
返回{mCurrentPath};
}
}
Mac版本是显而易见的

您还可以在单个cpp文件中使用此宏,如果您不知道如何进行更大的清理,则可以通过这种方式将它们包含在其中

旁注:
  • 还有
    boost::filesystem
    有这样的api,使用
    boost::filesystem::path
    非常方便
  • C++17引入了
    std::filesystem
    ,但它还没有得到很好的支持(例如MacOS)
  • 如果您将此功能封装在类中,您将为更好的测试(模拟)开辟道路

return*(新的std::string)
是泄漏内存的极好方法。您正在分配一个指针,该指针会立即丢失其地址,然后进行复制,并且永远不会删除并因此泄漏。哪一行代码导致调用“getcwd”时没有匹配的函数?在实际代码中,您是调用
getcwd
还是
futils::getcwd
?另外,如果可以使用C++17,则可以使用
return*(newstd::string(mCurrentPath))--这可以是
返回mCurrentPath。这会消除记忆。@CoryKramer,谢谢你的帮助tip@NathanOliver哇,我不知道
return*(新std::string)
是泄漏内存的好方法。您正在分配一个指针,该指针会立即丢失其地址,然后进行复制,并且永远不会删除并因此泄漏。哪一行代码导致调用“getcwd”时没有匹配的函数?在实际代码中,您是调用
getcwd
还是
futils::getcwd
?另外,如果可以使用C++17,则可以使用
return*(newstd::string(mCurrentPath))--这可以是
返回mCurrentPath。这会消除记忆。@CoryKramer,谢谢你的帮助tip@NathanOliver哇,我不知道
#include <stdio.h>  /* defines FILENAME_MAX */
#include <string>
#ifdef WINDOWS
    #include <direct.h>
    #define GetCurrentDir _getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
 #endif

namespace fUtils{
    std::string getcwd(){
    char mCurrentPath[FILENAME_MAX];
    GetCurrentDir(mCurrentPath, sizeof(mCurrentPath));
    return *(new std::string (mCurrentPath));

    }
}