Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I design this code to read files from a directory and put them into a list. The problem I have is that it will not put the files in the list. I keep getting the error message

Error (active) E0304 no instance of overloaded function "std::list<_Ty, _Alloc>::push_back [with _Ty=std::string, _Alloc=std::allocator<std::string>]" matches the argument list

How can I over come this?

C++
#include <iostream>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<fstream>
#include<string>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;

string path = "C:/Users/deonh/Downloads/intranets/intranet1"; //This gets every single file in the directory

string path5 = "C:/Users/deonh/Downloads/intranets/intranet5";
string path7 = "C:/Users/deonh/Downloads/intranets/intranet7";
<pre>int main()
{
	list<string>pages;
	map<string, int> page;


	//Here I am printing the files to make sure the above code works.

	for (const auto& entry : fs::directory_iterator(path)) {

		cout<< entry.path()<< std::endl;
		
		pages.push_back(entry.path());

	}
	for (list<string> ::iterator it = pages.begin(); it != pages.end(); it++) {
		cout << *it << endl;
	}



	return 0;
}



What I have tried:

I change
pages.push_back(entry.path());
to
pages.push_back(path);
and that gives me the directory, but does not include the files.
Posted
Updated 29-Sep-19 21:00pm

1 solution

The directory_iterator[^] enumerates over the directory_entry[^] collection. The path()[^] operator of each directory entry returns a std::filesystem::path[^] object. You can use the string()[^] function of the path object to get its string representation:
C++
pages.push_back(entry.path().string());
 
Share this answer
 
Comments
CPallini 30-Sep-19 5:31am    
5.
phil.o 30-Sep-19 6:50am    
Thank you Carlo :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900