Hi,
I've actually checked everything many times. I really don't know where the problem is!
Errros:
undefined reference to `read(std::istream&, Student_info&)'
undefined reference to `grade(Student_info const&)'
The above-mentioned functions (read and grade) are linked to the main program using headers and source files. If it helps, I am learning partitioning via Accelerator C++ book, chapter4. The main code is just 60 lines.
Please note that read is defined in "student_info" and grade in "grade". (lines : 9, 10, 11 and errors in : 32, 50)
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "Student_info.h"
#include "grade.h"
#include "median.h"
using std::cin;
using std::cout;
using std::domain_error;
using std::max;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;
using std::endl;
int main ()
{
vector <Student_info> students;
Student_info record;
string::size_type maxlen = 0; // the length of the longest name
// read and store all the students data
while (read (cin, record)) {
// find length of longest name
maxlen = max (maxlen, record.name.size());
students.push_back(record);
}
//alphabetize the stduent records
sort (students.begin(), students.end(), compare);
// write the names and grades
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;
}
During build object code should be compiled from each .cpp file.
Then the linker creates the executable by combining the object files (and libraries).
If your project is not set to compile every .cpp and link all the results, or if no .cpp provides implementation for function, then you get the undefined reference.
I do have Student_info.cpp , grade.cpp, and median.cpp. Isn't it enough to keep all the source files and related headers in the same folder you are running your main file?
******
Student_info.h:
#ifndef GUARD_Student_info
#define GUARD_Student_info
istream& read (istream& is, Student_info& s)
{
// read and store the student's name and midterm and final exam gradees
is >> s.name >> s.midterm >> s.final;
//read and store all the student's homework grades
read_hw (is, s.homework);
return is;
}
//read homework grades from an input stream into a vector
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
> Isn't it enough to keep all the source files and related headers in the same folder you are running your main file?
No, you actively have to add the source files to your project, so your IDE knows that they need to be compiled as part of your program.
The problem is solved:
Firstly:
I defined all the files under a project (instead of just keeping all of them in a single doc).
Secondly:
After you are done with all .h s and .cpp. s, you must go to project options > Files . Then choose the .cpp s and for each of them you should check these boxes: include in compilation, include in linking, and compile file as a C++
Regards