1

I have a small snipped of code, which just produces a function to get the current directory for either Windows or Linux platform:

#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));

    }

This is all great and working; however, I'd like to make the getcwd() function inside the namespace, fUtils, hence I did this:

#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));

    }
}

But this gives an error in VSCode which says:

no matching function for call to 'getcwd'

What mistake am I making in this? If this isn't how I put the function to the namespace fUtils, then how should I put it into the namespace?

6
  • 9
    return *(new std::string) is an excellent way to leak memory. You are allocating a pointer that you immediately lose the address of, then copy anyway, and never delete and therefore leak. Jun 2, 2020 at 12:58
  • 2
    What line of code causes no matching function for call to 'getcwd'? In your actual code, do you call getcwd or fUtiils::getcwd? Also, if you can uses C++17 you can use current_path instead Jun 2, 2020 at 12:58
  • 2
    return *(new std::string (mCurrentPath)); -- This can simply be return mCurrentPath;. This gets rid of the memory. Jun 2, 2020 at 13:00
  • @CoryKramer , thanks for the super helpful tip
    – juztcode
    Jun 2, 2020 at 16:59
  • @NathanOliver , wow, I didn't know that
    – juztcode
    Jun 2, 2020 at 17:00

2 Answers 2

7

Your fUtils::getcwd() function is attempting to call itself when the GetCurrentDir macro is evaluated (to getcwd), and this results in a function that expects no argument but is being given two arguments.

To resolve this, add the global namespace operator (::) in the definitions for GetCurrentDir, as follows:

#ifdef WINDOWS
    #include <direct.h>
    #define GetCurrentDir ::_getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir ::getcwd
#endif

Then, in your function body, it is clear to the compiler that your aren't looking for a 'recursive' (and invalid) call.

0
2

Use of macros in such context is invitation to problems in future.

Just wrap those functions with own API and all problems will be resolved.

Header file:

#include <string>

namepsace fUtils {
std::string getcwd();
}

Then you can have platform specific cpp files, Windows:

namepsace fUtils {
std::string getcwd() {
    char mCurrentPath[FILENAME_MAX];
    ::_getcwd(mCurrentPath, sizeof(mCurrentPath));
    return {mCurrentPath};
}
}

Mac version is obvious.

You also can use this macros inside single cpp file and this way contain them there if you do not what to do larger clean up.

Side notes:

  • there is boost::filesystem which has such api also use of boost::filesystem::path is quite handy
  • C++17 introduces std::filesystem, but it is not well supported yet (for example MacOS)
  • if you wrap this functionality in classes you will open your way for better testing (mocks).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.