Array Issue

Can someone please compile my code and see what the problem I am getting an undefined reference error and I am just stumped on what to do I have tried about everything. The only file I allowed to edit is students.cpp
to use the function if it compiles = ./registration courseList.dat enrollmentList.dat outputReport.csv

Error I am getting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sirius: make
touch registration.d
touch courses.d
touch students.d
cat registration.d courses.d students.d > make.dep
g++ -g -DUnix -MMD -o registration.o -c registration.cpp
g++ -g -DUnix -MMD -o courses.o -c courses.cpp
g++ -g -DUnix -MMD -o students.o -c students.cpp
g++ -g -DUnix  -o registration registration.o courses.o students.o 
registration.o: In function `printReport(std::ostream&)':
/home/x/cs333/array/registration.cpp:53: undefined reference to `students'
/home/x/cs333/array/registration.cpp:54: undefined reference to `students'
/home/x/cs333/array/registration.cpp:55: undefined reference to `students'
/home/x/cs333/array/registration.cpp:57: undefined reference to `students'
/home/x/cs333/array/registration.cpp:56: undefined reference to `students'
collect2: error: ld returned 1 exit status
make: *** [registration] Error 1


courses.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "arrayUtils.h"
#include "courses.h"

using namespace std;


int numCourses;
Course* courses; // array of courses


// Initialize all course data, allowing for at least the indicated
//   number of courses
void initializeCourses(int maxCourses)
{
  numCourses = 0;
  courses = new Course[maxCourses];
}


// Clean up course data
void cleanUpCourses()
{
  delete [] courses;
}


// Read course info from an input stream
void readCourses (std::istream& in)
{
  int maxCourses;
  in >> maxCourses;
  initializeCourses (maxCourses);
  string name;
  while (in >> name)
    {
      int credit;
      in >> credit;
      //cerr << name << " " << credit << endl;
      addCourse (name, credit);
    }
}


// Search an array for a given value, returning the index where 
//    found or -1 if not found.
int seqSearch(const Course list[], int listLength, std::string searchItem)
{
    int loc;

    for (loc = 0; loc < listLength; loc++)
        if (list[loc].name == searchItem)
            return loc;

    return -1;
}




// Add a course with the indicated number of credits
void addCourse (std::string courseName, int credits)
{
  courses[numCourses].name = courseName;
  courses[numCourses].credits = credits;
  ++numCourses;
}

// Get the number of credits for a course
int getCredits (std::string courseName)
{

  int pos = seqSearch (courses, numCourses, courseName);
  if (pos >= 0)
    return courses[pos].credits;
  else
    return 0;
}




courses.h
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
#ifndef COURSES_H
#define COURSES_H

#include <iostream>
#include <string>


struct Course {
  std::string name; // course name
  int credits;      // number of credits this course is worth
};

extern int numCourses;
extern Course* courses; // array of courses


// Initialize all course data, allowing for at least the indicated
//   number of courses
void initializeCourses(int maxCourses);

// Clean up course data
void cleanUpCourses();


// Read course info from an input stream
void readCourses (std::istream& in);

// Add a course with the indicated number of credits
void addCourse (std::string courseName, int credits);

// Get the number of credits for a course
int getCredits (std::string courseName);

#endif


students.cpp
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

//Array_Manipulation Asst

#include "arrayUtils.h"
#include "students.h"

using namespace std;

int numStudents;

std::string* studentNames;

int* studentCredits;

void initializeStudents(int maxStudents)
{
    numStudents = 0;
  studentNames = new string[maxStudents];
  studentCredits = new int[maxStudents];

}

int addStudent (std::string studentsName)
{
    int student = binarySearch(studentNames, numStudents, studentsName);

    if (student >= 0)
    {
        return student;
    }

    student = addInOrder(studentNames, numStudents,studentsName);
    addElement(studentCredits, --numStudents,student, 0 );

    return student;
}

int addCredits (std::string studentName, int credits)
{
    int student = addStudent(studentName);
    studentCredits[student] += credits;
    return studentCredits[student];
}

void cleanUpStudents()
{
    delete [] studentNames;
    delete [] studentCredits;
}


students.h
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
#ifndef STUDENTS_H
#define STUDENTS_H

#include <string>

struct Student {
  std::string name;  // name of this student
  int credits;       // total number of credits for this student
};

extern int numStudents;

extern Student* students; // array of students


// Initialize all student data, allowing for at least the indicated
//   number of students
void initializeStudents(int maxStudents);

// Clean up student data
void cleanUpStudents();


// Add a student, starting with 0 credits. Return position where
//   inserted.
int addStudent (std::string studentsName);

// Add a number of credits taken by a student
int addCredits (std::string studentName, int credits);


#endif


registration.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


#include "courses.h"
#include "students.h"


using namespace std;



/*
 * Read the enrollment requests, processing each one and tracking
 * the number of students successfully enrolled into each course.
 */
void processEnrollments (istream& enrollmentRequestsFile)
{
  int maxStudents;
  enrollmentRequestsFile >> maxStudents;
  initializeStudents (maxStudents);

  string courseName;
  enrollmentRequestsFile >> courseName;
  while (enrollmentRequestsFile) {
    enrollmentRequestsFile >> ws;
    string studentName;
    getline (enrollmentRequestsFile, studentName);

    //cerr << studentName << " " << courseName << endl;

    int credits = getCredits (courseName);
    addCredits (studentName, credits);

    enrollmentRequestsFile >> courseName;
  }
}


// Print an alphabetical list of students, with the
// total number of credits taken by each.
void printReport (ostream& out)
{
  string padding (30, ' ');
  string header = "Student" + padding;
  header = header.substr(0,30) + " Credits";
  cout << header << endl;
  int totalCredits = 0;
  for (int i = 0; i < numStudents; ++i)
    {
      string line = students[i].name + padding;
      cout << line.substr(0,30) << " " << students[i].credits << endl;
      totalCredits += students[i].credits;
      out << '"' << students[i].name << '"' << ","
	  << students[i].credits << endl;
    }
  cout << numStudents << " students registered for a total of " 
       << totalCredits << " credits." << endl;
}


int main (int argc, char** argv)
{
  if (argc != 4) 
    {
      cerr << "Usage: " << argv[0] << " courseFile enrollmentFile reportFile" << endl;
      return -1;
    }

  // Take input and output file names from the command line
  ifstream coursesIn (argv[1]);
  readCourses (coursesIn);
  coursesIn.close();


  ifstream enrollmentIn (argv[2]);
  processEnrollments (enrollmentIn);
  enrollmentIn.close();


  ofstream reportOut (argv[3]);
  printReport (reportOut);
  reportOut.close();

  cleanUpCourses();
  cleanUpStudents();


  return 0;
}


  

courses0.dat
1
2
3
4
5
6
4
CS150  4
CS252  1
CS300U 3
ENGL200 3

enrollments0.dat
1
2
3
4
5
6
7
8
9
10
11
12
13
10
CS150  Aiken, Roberto
CS150  Al-douli, Gustavo
CS150  Bach, Ruth
CS150  Blankenship, Kelly
CS150  Cancado, Fahad
CS300U  Franklin, Clifford
CS300U  Aiken, Roberto
CS252  Aiken, Roberto
CS252  Blankenship, Kelly
ENGL200  Blankenship, Kelly
ENGL200  Beidel, Lance
 
Last edited on
> I am getting an undefined reference error

Post the text of the error message.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sirius: make
touch registration.d
touch courses.d
touch students.d
cat registration.d courses.d students.d > make.dep
g++ -g -DUnix -MMD -o registration.o -c registration.cpp
g++ -g -DUnix -MMD -o courses.o -c courses.cpp
g++ -g -DUnix -MMD -o students.o -c students.cpp
g++ -g -DUnix  -o registration registration.o courses.o students.o 
registration.o: In function `printReport(std::ostream&)':
/home/x/cs333/array/registration.cpp:53: undefined reference to `students'
/home/x/cs333/array/registration.cpp:54: undefined reference to `students'
/home/x/cs333/array/registration.cpp:55: undefined reference to `students'
/home/x/cs333/array/registration.cpp:57: undefined reference to `students'
/home/x/cs333/array/registration.cpp:56: undefined reference to `students'
collect2: error: ld returned 1 exit status
make: *** [registration] Error 1
You have not defined extern Student* students; // array of students

Add this line to students.cpp
Student* students = 0 ; // define students
Thanks! I was able to solve it. I
Topic archived. No new replies allowed.