[Linker error] -- won't compile

Hey, I'm having a little trouble with my program. I asked my friend, and the teacher's assistant and they both can't help. Can anyone see the problem with my program?

I've written the whole thing, but I get this damn Linker error. My friend said all the code looks legit, but... It still doesn't compile. I have the feeling it's something very simple.

Thanks.

Error:
1
2
3
  [Linker error] undefined reference to `AVG(char)' 
  [Linker error] undefined reference to `max_min(char, int)' 
  ld returned 1 exit status 

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
/*****************************
 *  Made by me               *
 *  Assignment 3             *
 *  ece270                   *
 *****************************/

#include <iostream>
#include <fstream>
using namespace std;

int AVG(char);
int max_min(char,int);

int main()
{
    char filename[50] = "a3.txt";
    int count = AVG(filename[50]);
    
    max_min(filename[50], count);
    
    return 0;
}

int AVG(char filename[50])
{
    int current_number = 0;
    int total_ = 0;
    int count = 0;
    
    ifstream the_file;
    the_file.open (filename);

    while(the_file)
    {
         the_file >> current_number;
         total_ += current_number;
         
         count++;    
    }
    cout << total_ << " is the total of all numbers in the file." << "\n";
    cout << count << " is the number of number in the file." << "\n";
    cout << (total_ / count) << " is the average of all number in the file." << "\n";
    the_file.close();
    
    return count;
}

int max_min(char filename[50], int count)
{
    ifstream the_file;
    the_file.open (filename);
    
    int current_number;
    int max, min;
    
    for(int i = 1; i<count; i++)
    {
            the_file >> current_number;
            if(current_number > max) max = current_number;
            if(current_number < min) min = current_number;
    }
    cout << max << " is the highest number in this file." << "\n";
    cout << min << " is the lowest number in this file." << "\n";
    
    the_file.close();
    return 0;
}
Your problem is with a misunderstanding of char arrays.

Save yourself some headaches and use std::string instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>  // don't be afraid of string
using namespace std;

int AVG(string);  // use strings
int max_min(string,int);

int main()
{
    string filename = "a3.txt";
    int count = AVG(filename);  // better
    
    max_min(filename, count);  // better
    
    return 0;
}

//...

int max_min(string filename,int count)
{
    ifstream the_file;
    the_file.open (filename.c_str());  // call .c_str() when you need to get a char array 




If you can't use strings for whatever reason, then you can make the following changes:

1
2
3
4
5
6
7
8
9
10
11
// make sure your parameter types match

int AVG(char filename[50]);   // <- char [50] , not char
int max_min(char filename[50],int);  // <- same

int main()
{
    char filename[50] = "a3.txt";
    int count = AVG(filename);  // don't put [50] here, pass the pointer, not just one char
    
    max_min(filename, count);  // same 
Line 12 - function declaration int max_min(char,int);

and line 48 - function definition int max_min(char filename[50], int count) are inconsistent.
@Disch: I'm a beginner to but shouldn't line 9 on your second code block (In case he cant use strings) read as:

int count = AVG(*filename);?

Otherwise it doesn't compile\work for me. Even then it only seems to pass one char at a time outside the function. If he can't use strings would a global char array be a better option so that every function can see it?
Last edited on
Thanks, you guys were really fast. My program works now. :)
@ Computergeek:

No, my code is correct. *filename is bad because it only passes the first character of the array, not a pointer to the whole array (which would in a sense pass the whole string).

if it wasn't compiling for you, make sure your function prototype / definition matched mine. Notice I'm never having char as a parameters, always a char array: char filename[50]. That makes a big difference.

If he can't use strings would a global char array be a better option so that every function can see it?


Absolutely not. Globals are almost never the better option.
Topic archived. No new replies allowed.