Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STL: remove_copy_if in a Class

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

I was happy that I found a STL way to copy elements using a functional condition :

Code:
bool FileNotExists(string Filename)
{
	return (FileExists(Filename.c_str()) == FALSE);
}


int main()
{
	vector<string> filenames;
	
	filenames.push_back( string("C:\\File1.txt") );
	filenames.push_back( string("C:\\File2.txt") );
	filenames.push_back( string("C:\\File3.txt") );
	filenames.push_back( string("C:\\File4.txt") );
	filenames.push_back( string("C:\\File5.txt") );
	filenames.push_back( string("C:\\File6.txt") );


	vector<string> existingFiles;

	// get a vector of files that Exist on the hard disk.  
	// Note: the remove_copy_if only copies elements where 
	// the function returns false, therefore use FileNotExists

	remove_copy_if( filenames.begin(), filenames.end(), 
					inserter(existingFiles, existingFiles.begin()), 
					FileNotExists );
}

However, when I attempted to encapsulate this into a class I ran into problems :

Code:
FileExistsCopy::FileExistsCopy(vector<string>& filenames)
{
    m_CurrentDirectory = getCurrentDirectory();

    remove_copy_if(filenames.begin(), filenames.end(), 
					inserter(m_existingFiles, m_existingFiles.begin()), 
					FileNotExists);
}

bool FileExistsCopy::FileNotExists(string Filename)
{
    // build the path using m_CurrentDirectory
    string strFilename = m_CurrentDirectory + string("\\") + Filename;
   
    return (FileExists(strFilename) == FALSE);
}

string FileExistsCopy::getCurrentDirectory() const
{
    // ...
}

This gives the compile error :
'FileExistsCopy::FileNotExists': function call missing argument list; use '&FileExistsCopy::FileNotExists' to create a pointer to member.

This is because FileNotExists is not static, but I need to use the member data in the static function.

Any Suggestions ?


Regards,
Rich.




Programmers are tools for converting caffeine into code
 
Move FileNotExists() into a functor class and pass m_CurrentDirectory to the functor constructor.
Code:
class FileNotExists
{
public:
   FileNotExists( const string&  curDir )
   : m_CurrentDirectory( curDir )
   {}

   bool operator()( const string&  Filename )
   {
       // build the path using m_CurrentDirectory
       string strFilename = m_CurrentDirectory + string("\\") + Filename;
   
       return (FileExists( strFilename ) == false);
   }

private:
   string  m_CurrentDirectory;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top