undefined reference to

Hello,

I have many .h and .cpp programs linked into a main to generate a computation on a vector filled with student grades.
I have tried g++ main.cpp grades.cpp Student_info.cpp median.cpp and I don't get a notice of error.
I get the following error:

ja@ja-desktop:~/StudentReports$ g++ main.cpp -o main
/tmp/ccIEOX7Y.o: In function `main':
main.cpp:(.text+0x77): undefined reference to `read(std::basic_istream<char, std::char_traits<char> >&, Student_info&)'
main.cpp:(.text+0xc3): undefined reference to `compare(Student_info const&, Student_info const&)'
main.cpp:(.text+0x191): undefined reference to `grade(Student_info const&)'
collect2: ld returned 1 exit status


My files are :
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
63
#include<algorithm>
#include<iomanip>
#include<ios>
#include<iostream>
#include<stdexcept>
#include<string>
#include<vector>
//main.cpp

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


using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::max;
using std:: setprecision;
using std:: sort;
using std:: streamsize;
using std:: string;
using std:: vector;

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.
// Invariant: students contains all the student records read so far
//maxlen contains the length of the longest name in students
	
	while (read(cin, record)) 
	{
		// find length of longest name
		maxlen = max(maxlen, record.name.size() );
		students.push_back(record);
	}
// alphabetize the student 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;
}


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
#include <stdexcept>
#include <vector>

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

using std:: domain_error;
using std:: vector;
//grade.cpp

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

double grade ( double midt, double final, double hw)
{
	return 0.2 * midt + 0.4* final + 0.4*hw;
}

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


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
#include "Student_info.h"

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

//Student_info.cpp

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 name, midterm and exam
	is >> s.name >> s.midterm >> s.final;
	read_hw(is, s.homework);
	return is;
}

	//read_hw is defined
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;
} 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <stdexcept>
#include <vector>
//median.cpp
using std:: domain_error;
using std:: sort;
using std:: vector;

	//this function copies the entire argument vector vec as opposed to using
	//only *vector<double> or vector<double>&
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];
}


Now, the .h files:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef GUARD_grade_h
#define GUARD_grade_h

//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


1
2
3
4
5
6
7
8
9
#ifndef GUARD_median_h
#define GUARD_median_h

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


#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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 


Any help will be appreciated. Thank you.
Last edited on
I don't see where you are having a problem. I ran
g++ main.cpp grades.cpp Student_info.cpp median.cpp -o main
on all your files, like you said you did and there was no problem compiling.

If you're saying that you're only having a problem when you run
ja@ja-desktop:~/StudentReports$ g++ main.cpp -o main
that's because you're not telling g++ to compile and link the other sources.
You are correct.

So, now all compiles like you said. However, when I enter the command:

./main
(the cursor stays here blinking)


Thank you. I see that the code I wrote is "mute". I need to add some string to describe what the code wants.

Topic archived. No new replies allowed.