//Prototype
void getChars (std::vector<std::string> *chars);
void getFileNames(std::vector<std::string> *fileList);
int main();
{
std::vector<std::string> characters;
getChars (&characters)
return 0;
}
//getChars
void getChars (std::vector<std::string> *chars)
{
std::vector<std::string> listOfFiles;
//Get the filenames
getFileNames(&listOfFiles);
chars->push_back (listOfFiles[0][1]); //I want to add the second character of the first file in listOfFiles but I get an error
//I can only get it to add the first element but not the second char within the first element.
}
//getFileNames Function
void getFileNames(std::vector<std::string> *fileList)
{
WIN32_FIND_DATA search_data;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile("Files\\*.txt", &search_data);
while(handle != INVALID_HANDLE_VALUE)
{
fileList->push_back(std::string(search_data.cFileName));
if(FindNextFile(handle, &search_data) == FALSE)
break;
}
}
See the comment on line 20, any ideas? cannot get the second character of the first element :( , writing the first element works fine though.
Remind me why you're using the dot operator in a function name again? :P push_back is your fix... Simple mistake.
Sorry I was typing it out manually very quick before I had to rush out was just a typo and not a reflection of the actual code, which does not work. I have edited the code in the OP
Passing the vector by ref would be better than passing it by pointer.
Could you give an example please? I am not well versed with vectors at all, trying to get better with them obviously (not been programming long!) in theory I guess there should be no problem accessing a char from another vector ?
Note that for in parameters you should use a const ref, but here as you're getting (it's an out parameter) you need a non-const ref.
Also, before you try to add a char to chars (line 20 of your orig code) you should check there is a first file (using vector<>::size or vector<>::empty).
//Prototype
void getChars (std::vector<std::string> &chars); // now a ref
void getFileNames(std::vector<std::string> &fileList); // now a ref
int main()
{
std::vector<std::string> characters;
getChars (characters); // don't use address-of operator
return 0;
}
//getChars
void getChars (std::vector<std::string> &chars) // now a ref
{
std::vector<std::string> listOfFiles;
//Get the filenames
getFileNames(listOfFiles); // don't use address-of operator
chars.push_back (listOfFiles[0][1]); // now using . rather than -> for push_back
}
// etc