0
static void GetFilesFromDir(std::string dir, std::vector<std::string>& items)
{
    WIN32_FIND_DATA findData;
    HANDLE hFind=FindFirstFile((dir+"\\*").c_str(), &findData); //1 error
    do
    {
        if(hFind != INVALID_HANDLE_VALUE)
        {
            std::string sFileName = findData.cFileName; //2 error
            LPCSTR lp(sFileName.c_str());

            if(sFileName == "." || sFileName == "..")
            {} //do nothing
            else if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                GetFilesFromDir(dir+"\\"+sFileName, items);
            else
                items.push_back(dir+"\\"+sFileName);
        }

    }   while (FindNextFile(hFind, &findData));
}

所以这是我刚刚从另一个项目复制到新项目的简单函数。它会无缘无故地抛出错误,尤其是因为它在其他项目中也有效......

1>c:\users\prog\documents\visual studio 2010\projects\vampire stealth\vampire    stealth\smallfunctions.h(22): error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

1>c:\users\prog\documents\visual studio 2010\projects\vampire stealth\vampire stealth\smallfunctions.h(27): error C2440: 'initializing' : cannot convert from 'WCHAR [260]' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

有人对出了什么问题有任何想法吗?我对此一无所知。

4

1 回答 1

1

第一个错误:

...(dir+L"\\*").c_str()...

第二个错误:

std::wstring wFileName = findData.cFileName;

我想你已经为项目设置了 UNICODE 标志,所以你需要使用宽字符。wchar_t 而不是 char 和 wstring 而不是字符串。

于 2012-12-15T20:49:30.080 回答