0
votes
// Finaldesktop.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <conio.h>


using namespace std;




int SearchDirectory(vector<string> &refvecFiles,
                const string        &refcstrRootDirectory,
                const string        &refcstrExtension,
                bool                     bSearchSubdirectories = true)
{
string     strFilePath;             // Filepath
string     strPattern;              // Pattern
string     strExtension;            // Extension
HANDLE          hFile;                   // Handle to file
WIN32_FIND_DATA FileInformation;         // File information


strPattern = refcstrRootDirectory + "\\*.*";


hFile = FindFirstFile(strPattern.c_str(), &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
  if(FileInformation.cFileName[0] != '.')
  {
    strFilePath.erase();
    strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

    if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
      if(bSearchSubdirectories)
      {
        // Search subdirectory
        int iRC = SearchDirectory(refvecFiles,
                                  strFilePath,
                                  refcstrExtension,
                                  bSearchSubdirectories);
        if(iRC)
          return iRC;
      }
    }
    else
    {
      // Check extension
      strExtension = FileInformation.cFileName;
      strExtension = strExtension.substr(strExtension.rfind(".") + 1);

      if(strExtension == refcstrExtension)
      {
        // Save filename
        refvecFiles.push_back(strFilePath);
      }
    }
  }
} while(FindNextFile(hFile, &FileInformation) == TRUE);

// Close handle
FindClose(hFile);

DWORD dwError = GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
  return dwError;
}

return 0;

}

int main()
{
int iRC = 0;
vector<string> vecAviFiles;
vector<string> vecTxtFiles;


// Search 'c:' for '.avi' files including subdirectories
iRC = SearchDirectory(vecAviFiles, "c:", "avi");
if(iRC)
{
cout << "Error " << iRC << endl;
return -1;
}

// Print results
for(vector<string>::iterator iterAvi = vecAviFiles.begin();
  iterAvi != vecAviFiles.end();
  ++iterAvi)
cout << *iterAvi << endl;

// Search 'c:\textfiles' for '.txt' files excluding subdirectories
iRC = SearchDirectory(vecTxtFiles, "c:\\textfiles", "txt", false);
if(iRC)
{
  cout << "Error " << iRC << endl;
  return -1;
}

// Print results
for(vector<string>::iterator iterTxt = vecTxtFiles.begin();
  iterTxt != vecTxtFiles.end();
  ++iterTxt)
cout << *iterTxt << endl;

// Wait for keystroke
_getch();

return 0;
}

And the error message:

error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &, const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'WCHAR [260]' c:\documents and settings\compaq\my documents\visual studio 2008\projects\finaldesktop\finaldesktop.cpp 41 Finaldesktop

2
Please post the relevant code, including at least several lines before and after the one listed in the error message, and the definitions of everything in the excerpt. - David Thornley
You would do well to accept the best answers to your previous questions, otherwise it just looks like you are ungrateful to all the freely given help to your problems. - David Heffernan
"My car's 'check engine' light is on, what's wrong with it?" - GManNickG
sorry sir, now i have added the code. Plz do look into it - suvirai
Now the title makes no sense because you have removed the "below mentioned error"! The complete message will have included a line number and a textual explanation, post it all, not just the error code! - Clifford

2 Answers

0
votes

Without seeing the code I can tell you one way to cause this would be to do something like this:

WCHAR wstr[260];
std::string temp;
temp + wstr;

You'll get lots of errors including the one above that you mentioned. However the problem in this case is that the binary operator for addition has no way to convert the WCHAR array you declared and passed into the function to a form that is combinable with std::string. The third line is really doing something like this if that makes no sense:

temp.operator+(wstr);

However if you were to do something like this you would be fine:

WCHAR wstr[260];
std::wstring temp;
temp + wstr;

The root problem is combining wide characters with ansi characters. If this was what is intended you need to convert the value you are going to pass in on the right hand side before calling operators or copy constructors. There are many ways to do this either by using std::copy or windows API like MultiByteToWideChar etc.

EDIT:

To focus on the original question your problem is in two places:

strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;
strExtension = FileInformation.cFileName;

WIN32_FIND_DATA struct defines it's cFileName member as:

TCHAR    cFileName[MAX_PATH];

See above answer for more detail but gist of it is std::string is basically a std::basic_string<char>

You will need to use WideCharToMultiByte windows API or some other conversion method before appending this to a std::string or use std::basic_string<TCHAR> or std::wstring instead.

2
votes

Looks to me like you're trying to concatenate a std::basic_string with a WCHAR [260]. But I have absolutely no idea without seeing some source. C++ error codes are notoriously cryptic.