Hello,

Having a problem with 2 classes: a class that is used to go through file directories and collect the file names in a vector, the other just creates an instance of this class. This code is from an older version of the program so I know it works. However, trying to break it down into smaller chucks (with classes etc) i'm getting an error when I try to assign:
vector<string> test = fileNames.processFilesNames();

error C2228: left of '.processFilesNames' must have class/struct/union

I'm not sure if C++ doesn't like this assignment or whether I've done something wrong.

Here's the code, any feedback would be most appreciated. Thanks!

TestProject.cpp

#include "stdafx.h"
#include <FileNames.h>
using namespace std;


int main(){

	FileNames fileNames();
	vector<string> test = fileNames.processFilesNames();
	return 0;
}

FileNames.h

#pragma warning(disable: 4786)
#include <io.h>
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;

class FileNames{
	public:

	vector<string> processFileNames(){
		vector<string> filePaths;
		collectFileNames(filePaths);				// Method call to collect file names in directories
		return filePaths;
	}				


	private:

		/*Structure to hold a directory and all its filenames.*/
		struct FILELIST{
			string filePath;
			vector<string> fileNameList;
		};


	/**##########collectFileNames(vector<string>)##########
	 * Calls the transverseDirectory() method to gather file names. Stores them in a list of strings
	 *
	 * @param &filePaths	- Reference to the string vector that is storing the file names
	 */
	void collectFileNames(vector<string> &filePaths){
	
		list<FILELIST> MyList;
		string path;
		cout << "Enter starting path ... ";
		getline(cin,path);
		transverseDirectory(path,MyList);
		list<FILELIST>::iterator it;

		for(it = MyList.begin(); it != MyList.end(); it++){
			vector<string>::iterator its;
			for(its = (*it).fileNameList.begin(); its != (*it).fileNameList.end(); its++){
				string fileName =  *its;
				int index_gts = fileName.find(".gts");
				if(index_gts == string::npos){
					continue;
				}else{
					filePaths.push_back((*it).filePath + "\\" + (*its));
				}
			}
		}
	}


	/**##########transverseDirectory(string, list<FILELIST>)##########
	 * Recursively goes through files and sub-directories
	 * 
	 */
	void transverseDirectory(string path, list<FILELIST> &theList){
	
		struct _finddatai64_t data;
		string fname = path + "\\*.*";
		long h = _findfirsti64(fname.c_str(),&data);
	
		if(h >= 0){
			FILELIST thisList;
			// add empty FILELIST structure to the linked list argument
			theList.push_back(thisList);
			// get pointer to the FILELIST just added to the linked list above.
			list<FILELIST>::iterator it = theList.end();
			it--;
			// set current path
			(*it).filePath = path;
			do{
				if( (data.attrib & _A_SUBDIR)){
					// make sure we skip "." and "..".  Have to use strcmp here because
					// some file names can start with a dot, so just testing for the 
					// first dot is not suffient.
					if( strcmp(data.name,".") != 0 &&strcmp(data.name,"..") != 0){
						// We found a sub-directory, so get the files in it too
						fname = path + "\\" + data.name;
						// recursion here!
						transverseDirectory(fname,theList);
					}
				}else{
					// this is just a normal filename.  So just add it to our vector
					(*it).fileNameList.push_back(data.name);
				}
			}while( _findnexti64(h,&data) == 0);
			// close the find handle.  
			_findclose(h);
		}
	}
};

Just realised I missed out #include <string> and <vector>. Doesn't make any difference

Have you tried changing #include <FileNames.h> to something more like #include "FileNames.h" ? The behavior of #include <> is different from the behavior of #include "".

Edited 6 Years Ago by Fbody: n/a

Just found my error. It was just the parentheses at the end of

FileNames fileNames();

Surely any other IDE would have but a red squiggley line under it?

Wow, I can't believe I missed that... I'm not so sure another IDE would have flagged it. Technically, that is a valid function declaration/signature/prototype and is valid syntax.

The issue is that it's inside main(), which isn't allowed. You can't declare a function inside of another function.

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.