2017-03-16 22 views
0

私はC++で学生の成績を保存するプログラムを構築しようとしています。コンパイルするとき、私は次のエラーを受信して​​います:C++ヘッダーファイル、アーキテクチャx86_64の未定義シンボル

Undefined symbols for architecture x86_64: "grade1(Student_records const&)", referenced from: 
_main in gradereport-a0007a.o 

"compare(Student_records const&, Student_records const&)", 

referenced from: _main in gradereport-a0007a.o 

"read_info(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Student_records&)", 

referenced from: _main in gradereport-a0007a.o 

ld: symbol(s) not found for architecture x86_64 

私は(しよう)されているコマンドをコンパイルするために使用する:G ++ gradereport.cpp

ザ・は私に言ったことについて何か私のREAD_INFO、比較し、グレード1の関数が間違っていた(不適切に定義されていた)。しかし、私は問題を見つけるように見えることはできません。

「未定義のシンボル」の問題を読んでから、(a)私の関数宣言が一致しないか、(b)複数のファイルを同時に「コンパイルする」必要があります。より良いフレーズ)。私はそれが(a)ではないと確信しています。 (b)の場合、この作業をどれくらい正確に達成できますか?また、基本的な批判があれば、それを聞いてみたいと思います。前もって感謝します。

ここに私のコードです:

gradereport.cpp

#include<iostream> 
#include<ios> 
#include<iomanip> 
#include<algorithm> 
#include<vector> 
#include<stdexcept> 
#include "student_records.h" 
#include "Grades.h" 

using std::cout; 
using std::endl; 
using std::vector; 
using std::cout; 
using std::cin; 
using std::string; 
using std::max; 
using std::domain_error; 

int main() 
{ 

    vector<Student_records> students; 
    Student_records record; 
    string::size_type maxlen = 0; 

    while(read_info(cin, record)) 
    { 

     maxlen = max(maxlen, record.name.size()); // finds the longest name 
     students.push_back(record); // enters each record into the vector 

    } 

    sort(students.begin(), students.end(), compare); 
    for(vector<Student_records>::size_type i = 0; i != students.size(); ++i) 
    { 

     cout << students[i].name 
      << string(maxlen + 1 - students[i].name.size(), ' '); 

     try 
     { 

      double final_grade = grade1(students[i]); 
      /* need to define grade function: 
       1) unpack grades; 
       2) find homework grades; 
       3) find final grade; 
      */ 
      cout << final_grade 

     } 

     catch(domain_error e) 
     { 

      cout << e.what(); 

     } 

     cout << endl; 

    } 

    return 0; 

} 

student_records.h

#ifndef GUARD_STUDENT_RECORDS 
#define GUARD_STUDENT_RECORDS 

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

struct Student_records 
{ 

    std::string name; 
    double midterm, final; 
    std::vector<double> homeworks; 

}; 

bool compare(const Student_records&, const Student_records&); 
std::istream& read_info(std::istream&, Student_records&); 
std::istream& read_homework(std::istream&, std::vector<double>&); 

#endif 

student_records.cpp

#include "student_records.h" 

bool compare(const Student_records& x, const Student_records& y) 
{ 

    return x.name < y.name; 

} 

std::istream& read_info(istream& is, Student_records& s) 
{ 

    std::cout << "Enter name, midterm, and final: "; 
    is >> s.name >> s.midterm >> s.final; 

    read_homework(std::istream&, std::vector<double>&); 

    return is; 

} 

std::istream& read_homework(std::istream& in, std::vector<double>& homework) 
{ 

    cout << "Enter homework: "; 

    if(in) 
    { 
     hw.clear() 

     double x; 
     while(in >> x) 
      homework.push_back(x); 

     in.clear(); 
    } 

    return in; 

} 

Grades.hは

#ifndef GUARD_GRADES 
#define GUARD_GRADES 

#include<iostream> 
#include<vector> 
#include<string> 
#include "student_records.h" 

double grade1(const Student_records&); 
double grade2(double, double, std::vector<double>); 
double median(std::vector<double>); 

#endif 

Grades.cpp

#include<stdexcept> 
#include<vector> 
#include "grades.h" 
#include "Student_records.h" 

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

double grade2(double midterm, double final, std::vector<double> homework) 
{ 

    double final_homework_grade; 

    final_homework_grade = median(homework); 

    return .4 * final_homework_grade + .4 * final + .2 * midterm; 

} 

double median(vector<double> homework) 
{ 

    typedef vector<double>::size_type vec_sz; 

    vec_sz size = homework.size(); 

    if(size == 0) 
     throw domain_error("student has done no homework"); 

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

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

} 

double grade1(const Student_records& s) // unpacks grades 
{ 

    return grade2(s.midterm, s.final, s.homework); 

} 
+0

'student_records.cpp'で実装されている関数がないファイル(' gradereport.cpp')のみをコンパイルしています。 [G ++を使用して複数の.cppと.hファイルをコンパイルする](http://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files) – ssell

+0

ありがとうございました!期待しています – hbye5111

答えて

1

問題は既にこのforumで解決されています。

すべての.cppファイルまたは.ccファイルをコンパイルしていないため、コンパイラがヘッダーファイルによって追加されたインクルージョンを認識できないため、問題が発生します。

私はこれが誰かを助けることを願っています。

関連する問題