This is actually the program my instructor had provided with us to work with, i had to make several changes thus far to get it to at least compile:
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <string>
#include <list>
using namespace std;
std::string makeString();
std::string path= "/";
DIR*dirp;
struct dirent*direntp;
std::list<string> child_directories;
void dir_traverse(&path, ios::out){
list<string> child_directories;
DIR*dirp = opendir(path.data());
struct dirent*dir_entry = readdir(dirp);
while(dir_entry !=null){
unsigned char d_type = dir_entry->d_type==DT_OIR?'D' : 'R';
if(d_type == 'D'){
if(dir_entry->d_name[0]!= '.')
{
child_directories push_back(dir_entry_d_name);
}
}
out<<'\t'<<d_type<<":"<<dir_entry->d_name<<endl;
dir_entry= readdir(dirp);
}
list<string>::iterator it = child_directories.begin();
while(it! = child_directories.end())
{
dir_traverse(&path + "/" + *it, out);
it++;
}
closedir(dirp);
}
I get the error I have as the title in reference to the dir_traverse function. I am pretty much interested in figuring out what is causing the problem and why. Half of what is going on in the function I don’t really get at the moment, which is probably why I am having this compiling issue. just not up to speed on c/c++ yet 🙂
thanks
edit:
void dir_traverse(string& path, ostream& out){
list<string> child_directories;
DIR*dirp = opendir(path.data());
struct dirent*dir_entry = readdir(dirp);
while(dir_entry !=NULL){
unsigned char d_type = dir_entry->d_type==DT_DIR?'D' : 'R';
if(d_type == 'D'){
if(dir_entry->d_name[0]!= '.')
{
child_directories.push_back(dir_entry_d_name);
}
}
out<<'\t'<<d_type<<":"<<dir_entry->d_name<<endl;
dir_entry= readdir(dirp);
}
list<string>::iterator it = child_directories.begin();
while(it != child_directories.end())
{
dir_traverse(&path + "/" + *it, out);
it++;
}
closedir(dirp);
}
Okay, so you need to make a few changes to your instructor’s sample to get it to compile.
I’m going to assume you’ve included the four headers needed to compile. I recommend you pull in just the following four symbols from the
stdnamespace, rather than the whole lot. Personally I prefer to refer tostd::stringin my code.Anyway, only the following three lines need to be fixed:
Should be…
Because you later pass in a temporary when you call the function recursively.
Should be…
This looks like a typo!
Should be…
This looks like a typo, too. This is where you create the temporary
pathstringthat I mentioned earlier. This must be passed as aconstreference (since it is not passed by value).As you practise more, you’ll become more attuned to your compiler’s error messages!
Good luck!