可能重複:
Undefined symbols for architecture i386:C++編譯錯誤 - 對建築x86_64的未定義符號
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include "CorpusExp.h"
int main(int argc, char* argv[]){
ifstream infile("../DATA.txt");
string train_dir; // The training data directory
infile>>train_dir;
train_dir=train_dir.substr(6,train_dir.length());
if (argc>1){
if (strcmp(argv[1],"-s")){ // enter into CorpusExploration mode
CorpusExp ce(train_dir,"all"); //<<=======LINE X!!!!!!!!
//ce.calculate();
if (argc>=3 && strcmp(argv[2],"-u")){ // check user stats
cout<<"shit";
}
else if (argc>=3 && strcmp(argv[2],"-m")){ // check movie stats
}else{ // check the all (default) stats
}
}else if(strcmp(argv[1],"-m")) {// enter into Recommendation mode
}
}
return 0;
}
如果我包括LINE X,在main.cpp中不會編譯,錯誤信息是:
Undefined symbols for architecture x86_64:
"CorpusExp::CorpusExp(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [recommend] Error 1
這裏是我的CorpusExp.cpp和CorpusExp.h:
/* CorpusExporater頭文件 用於統計目的
*/
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h> // the Linux library to read the directories
class CorpusExp{
public:
CorpusExp(const string&,const string&); // (dir_name, mode)
void calculate();
void display(ostream &out, const int&); // print out the id's stat information
private:
string _dir_name;
int _movie_count;
int _user_count;
int _movie_rated_count[5]; // times of movie rated as 1,2,3,4,5
float _movie_avg; // total moview average rates
string _mode; // the mode of current task
int _id; // the id of current task
};
/*CorpusExporater cpp file
Used for statistical purpose
*/
#include "CorpusExp.h"
CorpusExp::CorpusExp(const string &dir_name, const string &mode):
_dir_name(dir_name),
_mode(mode),
_movie_count(0),
_user_count(0)
{
}
void CorpusExp::calculate(){
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir(strdup(_dir_name.c_str()));
if (dpdf!=NULL){
while(epdf = readdir(dpdf)){
cout<<epdf->d_name<<endl;
}
}
}
void CorpusExp::display(ostream &out, const int &id){
}
我幾乎沒有新C++,所以我很困惑,不知道哪個部分導致了這個問題。如果我刪除行X,一切都將只是罰款,但如果我在主函數聲明CorpusExp類,那麼它沒有編譯......
這裏的編譯文件:
# Makefile for Movie Recommendation--- HW5 of Search Engine 11-641
# Yitong Zhou
# Nov 19, 2012
#source code directory:
src=src
#obj code directory:
obj=obj
recommend: $(obj)/main.o $(obj)/CorpusExp.o
g++ -o [email protected] $<
$(obj)/main.o: src/main.cpp src/CorpusExp.cpp
g++ -c $< -o [email protected]
$(obj)/CorpusExp.o: $(src)/CorpusExp.cpp $(src)/CorpusExp.h
g++ -c $< -o [email protected]
clean:
rm -rf obj/*.o recommend
什麼是編譯器調用的樣子? – Xeo
看來你並沒有編譯*並將你的'CorpusExp.cpp'文件鏈接到你的程序中。頭文件定義和包含允許'main.cpp'編譯,但是如果沒有編譯過的'CorpusExp.cpp'的代碼,你將會得到未解析的外部數據。 – WhozCraig
您是否正在編譯類似於: g ++ main.cpp CorpusExp.cpp -o程序 – Chimera