Click here to Skip to main content
15,886,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm getting and error that wstring is undefined. What is cusing this? How can it be fixed? Its on the line.....wstring sLastAccessTime;

C++
#include <string>
#include <vector>
#include <windows.h>
#include <time.h>
#include <map>

struct file_data 
{ 
    wstring sLastAccessTime; 
    __int64 nFileSize      ; 
}; 
 
int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map) 
{ 
    WIN32_FIND_DATA fd; 
    HANDLE h = FindFirstFile(searchkey,&fd); 
    if(h == INVALID_HANDLE_VALUE) 
    { 
        return 0; // no files found 
    } 
    while(1) 
    { 
        wchar_t buf[128]; 
        FILETIME ft = fd.ftLastWriteTime; 
        SYSTEMTIME sysTime; 
        FileTimeToSystemTime(&ft, &sysTime); 
        wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay); 
 
        file_data filedata; 
        filedata.sLastAccessTime= buf; 
        filedata.nFileSize      = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
 
        map[fd.cFileName]= filedata; 
 
        if (FindNextFile(h, &fd) == FALSE) 
            break; 
    } 
    return map.size(); 
} 
 
int main() 
{ 
    std::map<std::wstring, file_data> map; 
    GetFileList(L"C:\\Users\\DS\\Downloads\\*.zip", map); 
    GetFileList(L"C:\\Users\\DS\\Downloads\\*.txt", map); 
 
    for(std::map<std::wstring, file_data>::const_iterator it = map.begin(); 
        it != map.end(); ++it) 
    { 
        MessageBoxW(NULL,it->first.c_str(),L"File Name",MB_OK); 
        MessageBoxW(NULL,it->second.sLastAccessTime.c_str(),L"File Date",MB_OK); 
    } 
 
    return 0; 
}
Posted
Updated 29-Sep-11 14:53pm
v3
Comments
Sergey Alexandrovich Kryukov 29-Sep-11 19:08pm    
Format the code properly, escape < and >; see the garbage at the end of the code sample...
--SA
Chuck O'Toole 29-Sep-11 20:03pm    
and the exact compiler error code would help a lot
Member 7766180 29-Sep-11 21:15pm    
1 IntelliSense: identifier "wstring" is undefined

Just when I thought you were doing so well. Over in this other answer thread How To Get A Count Of Files[^]You are using the almost identical program. Our discussion over there indicates that you were successfully compiling, running, and getting outputs. That code also uses wstring.

Now you're over here, asking why wstring is undefined in the code. Better yet, ask yourself what you changed since the earlier program where it did work. Clearly, it didn't break all by itself, you changed something in your code and/or build. You'd figure that out a lot quicker by yourself than posting messages over here and waiting hours for somebody to respond.

So, question 1 is not why it doesn't work but why did it used to work and is now broken.
 
Share this answer
 
Comments
Chuck O'Toole 29-Sep-11 22:53pm    
And, as Philippe said, 'wstring' uses should include the 'std::' namespace qualifier. The answer to the question I posed is, you modified the existing code to include another 'wstring' data item (sLastAccessTime) but did not use 'std::wstring'. So that addition was the difference in the two programs that caused a working program to now not compile.
Sergey Alexandrovich Kryukov 30-Sep-11 0:51am    
Good points, my 5.
--SA
I think that you need to qualify the type with std as you have done in main function (that is write std::wstring).
 
Share this answer
 
Comments
Member 7766180 29-Sep-11 21:16pm    
Bingo!!!!! You da man!!!!! Thank you.
Sergey Alexandrovich Kryukov 30-Sep-11 0:55am    
Good catch, my 5. It was my first guess. When I saw this post I saw std::wstring in the qualified form; probably I did not spot the place when it wasn't qualified.

I would also suggest "using namespace std".
--SA
Stefan_Lang 30-Sep-11 6:34am    
I generally do not advise using 'using' statements. Namespaces help to keep the global namespace clean, but 'using' negates that advantage (at least locally). Namespace qualifiers in the code also help recognizing what libraries are being used.

Having said that, I wonder if I'm being too restrictive, and what others think about it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900