"Undefined reference" issue.

Hey everyone,

I'm still extremely new to C++ but I think I'm making progress. Whenever I try to compile I get "undefined reference to `initialize(std::basic_ifstream<char, std::char_traits<char> >&, std::string, int*"

I've looked over at my spelling, my syntax, and I see nothing as to why I would get this message. The input file only includes

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83

and I've already checked the spelling of it

Please give it a look.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

#include <iostream>
#include <fstream>

using namespace std;

void initialize(ifstream &, string, int[]);
double calcAverage(int);
char determineGrade (double);
void printStudent (string, double, char);
void countGrade (char);

int count = 0;

int main()
{
    cout << "Student, Grade, and Average Calculator" << endl;
    cout << endl;

    ifstream inputfile;
	 inputfile.open("input.txt");

    string name;     int grades[5];

    inputfile >> name;

    while (inputfile)
    {
    initialize(inputfile, name, grades);
    count++;
    }


    if(inputfile.fail())
    {
        cout<<"Input file invalid.";
        return 1;
    }

    inputfile>> name;
    cout << "Total Number of Students: " << count << endl;
    inputfile.close();
    return 0;
}


void initialize(ifstream &in, string name, int y)
{
    double average; char grade;
    average = calcAverage(y);
    grade = determineGrade(average);
    printStudent(name, average, grade);
}

double calcAverage(int y)
{
    double average; int sum = 0;
    sum += y;
    average = sum / 5;
    return average;
}

char determineGrade(double average)
{
    char A, B, C, D, F;

    if(average >= 90)
    {
        return 'A';
        countGrade(A);
    }
    else if(average >=80)
    {
        return 'B';
        countGrade(B);
    }
    else if(average >=70)
    {
        return 'C';
        countGrade(C);
    }
    else if(average>=60)
    {
        return 'D';
        countGrade(D);
    }
    else
    {
        return 'F';
        countGrade(F);
    }
}


void printStudent (string name, double average, char grade)
{
    cout << "Name: " << name << "Average grade is: " << average << "Final Grade: " << grade << endl;
}

void countGrade(char x)
{
    int A = 0, B = 0, C = 0, D = 0, F = 0;

    if (x = 'A')
        A++;
    else if (x = 'B')
        B++;
    else if (x = 'C')
        C++;
    else if (x = 'D')
        D++;
    else if (x = 'F')
        F++;

    cout << endl;
    cout << "Total A's: " << A << endl;
    cout << "Total B's: " << B << endl;
    cout << "Total C's: " << C << endl;
    cout << "Total D's: " << D << endl;
    cout << "Total F's: " << F << endl;
}
line 7 declares the 3rd parameter as an array of integers:
void initialize(ifstream &, string, int[]);

line 47 defines the 3rd paraeter as a single integer, not an array:
void initialize(ifstream &in, string name, int y)


which should it be? Well, line 29 passes the array grades so it looks like line 47 should be changed to agree, that is y should be an array.

the first parameter ifstream &in is not used at all??

the = (assignment operator) at lines 104 to 112 should be == test for equality.
The y on line 47 representing the grades array then gets passed into the calcAverage function, and that seems to be expecting a single integer. Although the code in the average makes me think you're expecting to add up all the individual grades in the array to then divide by 5?

average = calcAverage(y);

1
2
3
4
5
6
7
double calcAverage(int y)
{
    double average; int sum = 0;
    sum += y;
    average = sum / 5;
    return average;
}

Last edited on
Thank you both for your help, I totally overlooked that array error. I got it working now.
Topic archived. No new replies allowed.