2013-03-06 26 views
-2
#ifndef GUARD_student_info_h 
#define GUARD_student_info_h 

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

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.h頭文件C++與頭文件發放該未定義

#include "student_info.h" 

using std::istream; using std::vector; 

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; 
} 

這是student_info.cpp

#ifndef GUARD_median_h 
#define GUARD_median_h 

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

#endif 

這是median.h

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

using namespace std; 
using std::domain_error; 
using std::vector; 

double median(vector<double>vec) // No need of saying it as vector<double>homework, cos we wont be using this to always give median of hw, but median of some other data 
{ 
    typedef vector<double>::size_type vec_sz; 
    vec_sz size; 

    size = vec.size(); 

    if(size == 0) 
     throw domain_error("Empty homework. From medain.cpp when vec.size() == 0."); 

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

    vec_sz mid = size/2; 

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

這是median.cpp

#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.h

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

using namespace std; 
using std::domain_error; 

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 domain_error("No homework. From grades function 2 in grade.cpp."); 

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

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

這是grade.cpp

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

using namespace std; 
using std::streamsize; 
using std::setprecision; 
using std::domain_error; 
using std::max; 

istream& read(istream& , student_info &); 

int main() 
{ 
    vector<student_info>students; 
    student_info record; 
    string::size_type maxlen = 0; 

    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) { 
     // write the name, padded on the right to maxlen + 1 characters 
     cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' '); 
     // compute and write the grade 
    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; 
    } 

這是main.cpp中

當我編譯它,我得到以下錯誤:

undefined reference to `read(std::istream&, student_info&)' 


undefined reference to `compare(student_info const&, student_info const&)' 


undefined reference to `grade(student_info const&)' 
collect2: error: ld returned 1 exit status 

發生了什麼事。有人能糾正它,讓我知道是什麼問題。

+4

添加編譯器行。我想你只是忘了添加所有的源文件。此外,這是太多的代碼。 – Zeta 2013-03-06 13:55:10

+1

您不用所有源文件構建。請編輯您的問題以顯示您如何構建您的項目。 – 2013-03-06 13:56:14

+0

你從哪裏得到這些未定義的參考?在哪個文件? – 2013-03-06 13:56:19

回答

1

使用多個文件構建項目時,有幾種方法可以實現。一種是使用Integrated Development Environment(也稱爲IDE),它將自動處理構建過程以使用項目中的所有文件構建。

如果你想自己編譯的命令行,你可以直接編譯的所有文件:

$ gcc main.cpp grade.cpp median.cpp student_info.cpp -o my_program 

或者您單獨編譯所有文件,然後鏈接他們一起在一個單獨的步驟:

$ gcc main.cpp -c 
$ gcc grade.cpp -c 
$ gcc median.cpp -c 
$ gcc student_info.cpp -c 
$ gcc main.o grade.o median.o student_info.o -o my_program 

(該-c標誌來gcc告訴編譯器生成可以稍後被用來林對象文件 k已完成的程序)。

做單獨的編譯和鏈接可能很乏味,所以有一些程序可以幫助您自動化,如make。實際上,UNIX環境中的許多IDE(如Linux和OSX)會生成make所需的文件來處理構建過程,並呼籲make逐個構建文件,然後像上一個例子中那樣鏈接它們。

+0

我正在使用代碼塊IDE。我在編譯main。cpp文件按f9,當我編譯我得到上述錯誤。什麼是my_program?順便說一句如何拼寫你的名字,這是什麼意思 – user2111006 2013-03-06 14:09:44

+0

好吧請告訴我如何編譯和運行這個程序。我不明白 – user2111006 2013-03-06 14:14:37

+0

@ user2111006您是否已將其他源文件添加到項目中?他們正在建設嗎? '-o'選項告訴'gcc'命名生成的文件,所以在我的示例中,我使用它來命名完成的可執行文件'my_program'。 – 2013-03-06 14:16:37