2016-04-24 79 views
0

我正在通過加速C++,我無法從第4章獲得程序編譯;這是第一個標題。我也從here下載了源代碼,並得到了與手抄代碼完全相同的錯誤,所以我不確定問題所在。加速的C++:體系結構x86_64的未定義符號

我在OSX El Capitan上使用g ++。下面是我用的確切命令:

$ g++ main.cpp 

這裏的錯誤:

Undefined symbols for architecture x86_64: 
    "read(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Student_info&)", referenced from: 
     _main in grades-main-e0475a.o 
    "grade(Student_info const&)", referenced from: 
     _main in grades-main-e0475a.o 
    "compare(Student_info const&, Student_info const&)", referenced from: 
     _main in grades-main-e0475a.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

而這裏的代碼:

的main.cpp

#include <algorithm> 
#include <iomanip> 
#include <ios> 
#include <iostream> 
#include <stdexcept> 
#include <string> 
#include <vector> 
#include "grade.h" 
#include "Student_info.h" 

using namespace std; 

int main() { 

    vector<Student_info> students; 
    Student_info record; 
    string::size_type maxlen = 0; // length of the longest name 

    while (read(cin, record)) { 
    maxlen = max(maxlen, record.name.size()); 
    students.push_back(record); 
    } 

    sort(students.begin(), students.end(), compare); 

    for (vector<Student_info>::size_type i = 0; i != students.size(); i++) { 
    cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' '); 

    try { 
     double final_grade = grade(students[i]); 
     streamsize prec = cout.precision(); 
     cout << setprecision(3) << final_grade << setprecision(prec); 
    } catch (domain_error e) { 
     cout << e.what(); 
    } 
    cout << endl; 
    } 

    return 0; // success 
} 

grade.h

#ifndef GUARD_grade_h 
#define GUARD_grade_h 

#include <vector> 
#include "Student_info.h" 

double grade(double, double, double); 
double grade(double, double, const std::vector<double>&); 
double grade(const Student_info&); 

#endif 

grade.cpp

#include <stdexcept> 
#include <vector> 
#include "grade.h" 
#include "median.h" 
#include "Student_info.h" 

using namespace std; 

double grade(double midterm, double final, double homework) { 
    return 0.2 * midterm + 0.4 * final + 0.4 * homework; 
} 

double grade(double midterm, double final, const vector<double>& hw) { 
    if (hw.size() == 0) throw doman_error("student has done no homework"); 

    return grade(midterm, final, median(hw)); 
} 

double grade(const Student_info& s) { 
    return grade(s.midterm, s.final, s.homework); 
} 

Student_info.h

#ifndef GUARD_Student_info 
#define GUARD_Student_info 

#include <iostream> 
#include <string> 
#include <vector> 

struct Student_info { 
    std::string name; 
    double midterm, final; 
    std::vector<double> homework; 
}; 

bool compare(const Student_info&, const Student_info&); 
std::istream& read(std::istream&, Student_info&); 
std::istream& read_hw(std::istream&, std::vector<double>&); 

#endif 

Student_info.cpp

#include "Student_info.h" 

using namespace std; 

bool compare(const Student_info& x, const Student_info& y) { 
    return x.name < y.name; 
} 

istream& read(istream& is, Student_info& s) { 
    is >> s.name >> s.midterm >> s.final; 

    read_hw(is, s.homework); 
    return is; 
} 

istream& read_hw(istream& in, vector<double>& hw) { 
    if (in) { 
    hw.clear(); 

    double x; 
    while (in >> x) { 
     hw.push_back(x); 
    } 
    in.clear(); 
    } 
    return in; 
} 

median.h

#ifndef GUARD_median_h 
#define GUARD_median_h 

#include <vector> 

double median(std::vector<double>); 

#endif 

median.cpp

#include <algorithm> 
#include <stdexcept> 
#include <vector> 

using namespace std; 

double median(vector<double> vec) { 
    typedef vector<double>::size_type vec_sz; 

    vec_sz size = vec.size(); 
    if (size == 0) throw domain_error("median of an empty vector"); 

    sort(vec.begin(), vec.end()); 

    vec_sz mid = size/2; 

    return size % 2 == 0 ? (vec[mid] + vec[mid-1])/2 : vec[mid]; 
} 

非常感謝您的幫助,任何人!

+2

顯示命令你用來編譯上面的代碼 – gudok

+0

如果你正在使用g ++,爲什麼前面加上'clang:'? – kfsone

+0

@gudok done。我用'g ++ main.cpp' @kfsone,我沒有知道錯誤爲什麼以「clang」作爲前綴。我是一名Java開發人員,自20年前我進入高中以來,我還沒有編譯C++代碼 - 我相信當時我們使用了Borland。 – AutonomousApps

回答

1

正如@ gudok的有點神祕的評論所暗示的,這很可能是您的第一個帶有多個源文件的程序。你不能只編譯一個文件並獲得可執行文件;您必須編譯每個文件並將生成的目標文件鏈接在一起。要做到這一點最簡單的方法是這樣的:

g++ main.cpp grade.cpp student_info.cpp median.cpp 

如果在代碼中沒有錯誤,將產生在當前目錄下一個名爲「a.out的」可執行文件。

編輯:只注意到該錯誤消息來自「鐺」,而不是「G ++」這沒關係;如果你使用。‘鐺’,只是改變了命令行

clang++ main.cpp grade.cpp student_info.cpp median.cpp 
+0

謝謝,這正是我所需要的,除了我用'g ++',而不是'clang ++ '。如上所示,這是我的第一個C++程序,帶有多個源文件,所以我只是不知道編譯它的正確命令。 – AutonomousApps

相關問題