i'm reading files in directory , passing function, think i'm doing in wrong way, not able figure out.
here code first reads files in folder , send function further operations.
#include <dirent.h> #include <stdio.h> #include <vector> #include <string> #include <iostream> using namespace std; std::vector<std::string> filename; int main(void) { dir *d; struct dirent *dir; vector<string> filelist; int i=0; d = opendir("files"); if (d) { while ((dir = readdir(d)) != null) { i++; filelist.push_back(dir->d_name); } for(int i=0;i<filelist.size();i++) { cout<<filelist[i]<<endl; dosomething(filelist[i]); } closedir(d); } return(0); } int dosomething(filename) { //do }
error
main.cpp: in function ‘int main()’: main.cpp:29:28: error: ‘dosomething’ not declared in scope dosomething(filelist[i]); ^ main.cpp: @ global scope: main.cpp:37:26: error: cannot convert ‘std::vector<std::basic_string<char> >’ ‘int’ in initialization int dosomething(filename) { ^ main.cpp:37:28: error: expected ‘,’ or ‘;’ before ‘{’ token int dosomething(filename) { ^
since dosomething
function defined after main, not visible, causes first error. correct way @ least declare function first:
int dosomething(); //declaration int main() { dosomething(); //now function declared } //definition int dosomething() { }
now, second , third errors emited because didn't include filename
parameter's type in function definition. based on code, should string:
int dosomething(string filename) { }
i noticed that, while function returns int
, not using it's returned value. nevertheless, don't forget return
dosomething
, otherwise cause undefined behavior.
Comments
Post a Comment