1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <Windows.h>
#include <vector>
/* Note the new function signature...
* We are passing in a pointer to an existing vector
*/
//Prototype
void getFileNames(std::vector<std::string> *fileList);
int main()
{
std::vector<std::string> listOfFiles; // No pointer
getFileNames(&listOfFiles);
std::cout << "A list of .txt files:\n";
for (std::vector::iterator it = listOfFiles.begin() ; it != listOfFiles.end(); ++it)
{
std::cout << *it << '\n' ;
}
}
void getFileNames(std::vector<std::string> *fileList)
{
WIN32_FIND_DATA search_data;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile("C:\\textfiles\\*.txt", &search_data);
while(handle != INVALID_HANDLE_VALUE)
{
std::cout <<"Found file: " << search_data.cFileName << std::endl;
/* Here, you append another found file to the end of the vector.
* However, unlike arrays, you do NOT have to worry about resizing.
* The vector is resized for you!
*/
fileList->push_back(std::string(search_data.cFileName));
if(FindNextFile(handle, &search_data) == FALSE)
break;
}
/* Because you are passing vector as pointer to function, there is
* nothing to return, thus, we don't have to:
* (1) write all the file names to a vector
* (2) copy that vector to a new vector
* You'll just end up doing number 1.
*/
}
|
The error is:
1>------ Build started: Project: Testing vector, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\ben\documents\visual studio 2012\projects\programming exercises\hh project\testing vector\testing vector\source.cpp(19): error C2955: 'std::vector' : use of class template requires template argument list
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see declaration of 'std::vector'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see declaration of 'std::vector'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see declaration of 'std::vector'
1>c:\users\ben\documents\visual studio 2012\projects\programming exercises\hh project\testing vector\testing vector\source.cpp(21): warning C4552: '<<' : operator has no effect; expected operator with side-effect
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Its not declaring the vector when I make the changes you suggested , I also tried changing it from
std::vector<int>::iterator
to
std::vector<std::string>::iterator
which didnt work either , currently using whats in the source though. Sorry it probably seems basic and dumb for me to be getting this wrong but as I said I have no experience with vectors yet.