undefined reference to functions

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 #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;	
}
Last edited on
Please note that read is defined in "student_info" and grade in "grade". (lines : 9, 10, 11 and errors in : 32, 50)


You need to post that code.
> #include "Student_info.h"
> #include "grade.h"
> #include "median.h"

Do you also have source files called
Student_info.cpp
grade.cpp
median.cpp


If so, you need to add them to your project.
A rough guess would be something like project->settings->add source...

If you don't have those source files, then you need to write them.

"Undefined reference" is a linker error.

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

//Student_info.h header file
#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
*******************
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

**********************

median.h:
#ifndef GUARD_median_h
#define GUARD_median_h


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

#endif
********************
*******************
*****************
Student_info.cpp:
#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)
{
// 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();

}
return in;
}
************************************************
grade.cpp:
//source files needed for grade.h


#include <stdexcept>
#include <vector>

#include "grade.h"
#include "median.h"
#include "Student_info.h"

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

//definitions for the grade functions .... (3 grade functions are neede)

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

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

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

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

*********************************************************
median.cpp:
double median (vector<double> vec)
{
// function body:
typedef vector<double>::size_type vec_sz;

vec_sz size = vec.size();

if (size==0)
throw domain_error("median of an emoty vector");

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

vec_sz mid = size/2;

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

}


I forgot to say: I am using DevC++

Regards
> 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.

@ salem c,
How should I do that?
regards
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
Like I said, you need to do something like
project->settings->add source

I don't have your IDE for accurate details.

But it looks like this in code::blocks.
https://imgur.com/a/pkhzHc9
@salem c
Yes your hint made my mind
regards
Topic archived. No new replies allowed.