BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #72636同步于 2013/7/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

求助:STL库链接问题

fenixlee520
2013/7/16镜像同步1 回复
直接上代码,简单地读取一个文件夹下的所有文件,结果放在一个vector<string>结构里。 GetFiles.h: #ifndef _GETFILES_H_ #define _GETFILES_H_ #include<vector> #include<string> #include<time.h> #include<sys/stat.h> #include<iostream> using namespace std; const int MAX_DUR = 5*60; // class GetFile { public: GetFile() { cout<<"GetFile constructor"<<endl; //可以打印出来 } vector<string> GetFolders(const string &path); vector<string> GetFiles(const string &path, const string &postfix=""); bool IsPrepared(const string &path); bool IsFolder(const string &path); bool IsFile(const string &path); }; #endif GetFiles.cpp: #include"GetFiles.h" #include<string.h> #include<stdio.h> #include<stdlib.h> #include<dirent.h> #include<vector> using namespace std; vector<string> GetFile::GetFolders(const string &path) { cout<<"GetFile::GetFolders"<<endl; vector<string> folders; struct dirent* ent = NULL; DIR* pDir; pDir = opendir(path.c_str()); cout<<path.c_str()<<endl; while(NULL != (ent = readdir(pDir))) { string fullpath = path + "/" + ent->d_name; if(IsFolder(fullpath)) { if(strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) { folders.push_back(ent->d_name); } } } closedir(pDir); return folders; } vector<string> GetFile::GetFiles(const string &path, const string &postfix) { vector<string> files; struct dirent* ent = NULL; DIR* pDir; pDir = opendir(path.c_str()); while(NULL != (ent = readdir(pDir))) //readdir is equivalent to read { string fullpath = path + "/" + ent->d_name; //if(8 == ent->d_type) if(IsFile(fullpath)) { if(postfix == "" || strstr(ent->d_name, postfix.c_str()) != NULL) { files.push_back(ent->d_name); } } } closedir(pDir); return files; } bool GetFile::IsPrepared(const string &path) { struct stat st; stat(path.c_str(), &st); return time(0) - st.st_ctime >= MAX_DUR; } bool GetFile::IsFolder(const string &path) { struct stat st; int ret = stat(path.c_str(), &st); return ret>=0 && S_ISDIR(st.st_mode); } bool GetFile::IsFile(const string &path) { struct stat st; int ret = stat(path.c_str(), &st); return ret >= 0 && S_ISREG(st.st_mode); } main.cpp: #include"GetFiles.h" #include<vector> #include<iostream> using namespace std; int main(int argc, char* argv[]) { int i; vector<string> filename_vec; vector<string>::iterator iter; GetFile sven; string path = "XXXXX"; filename_vec = sven.GetFolders(path); //出错 iter = filename_vec.begin(); for(; iter != filename_vec.end(); iter++) { cout<<*iter<<endl; } system("PAUSE"); return 0; } windows devC++可运行 Gentoo Linux可以编译,链接失败: g++ main.cpp -o main In function `main': main.cpp:(.text+0xaf): undefined reference to `GetFile::GetFolders(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' collect2: ld returned 1 exit status 不知道是哪里出错了,求C++大神指点
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
fenixlee520机器人#1 · 2013/7/16
自己sf吧,g++少写了GetFiles.cpp,哎,丢人现眼