[VC.NET] 如何修复 "C2039: ‘GetCurrentDirectoryA()’ : is Not a Member of ‘System::IO::Directory’"问题

[VC.NET] 如何修复 "C2039: ‘GetCurrentDirectoryA()’ : is Not a Member of ‘System::IO::Directory’"问题


看到论坛上有人问到如何处理这样的问题。看了一下前辈的响应后才发现,原来这样的问题是由于Windows.h文件内,已定义了GetCurrentDirectory这个宏所导致。该宏会在编译时把GetCurrentDirectory给改为GetCurrentDirectoryA或是GetCurrentDirectoryW。

#if UNICODE
#define GetCurrentDirectory GetCurrentDirectorryW
#else
#define GetCurrentDirectory GetCurrentDirectorryA
#endif

因此使用类似下面的程序,在编译时就会发生错误。

#include "stdafx.h"
#include 
using namespace System;
using namespace System::IO;

int main(array ^args)
{
	Console::WriteLine(L"Dir: "+System::IO::Directory::GetCurrentDirectory()); // Error!
    return 0;
}

错误消息
image

解决办法可利用#undef去把该宏给取消。

#undef GetCurrentDirectory

像是

#include "stdafx.h"
#include 
using namespace System;
using namespace System::IO;

#undef GetCurrentDirectory

int main(array ^args)
{
	Console::WriteLine(L"Dir: "+System::IO::Directory::GetCurrentDirectory());
    return 0;
}

Link

  • How to Fix – "C2039: ‘GetCurrentDirectoryA()’ : is Not a Member of ‘System::IO::Directory’"