Linker Error in C++ code, does anyone know why I'm getting this?

This is the error I keep getting ...
[Linker error] C:\Users\family\AppData\Local\Temp\ccCnWKZb.o:BlackHawksRoster.cpp:(.text+0x15f): undefined reference to `buildArray(std::string, int, int, int)'
[Linker error] C:\Users\family\AppData\Local\Temp\ccCnWKZb.o:BlackHawksRoster.cpp:(.text+0x1de): undefined reference to `printArray(std::string, int, int, int, int)'
collect2: ld returned 1 exit status


The error says I have an undefined reference to two of the functions I created, but I have both the prototype and the header in the right places in my code. My main function is calling these two functions that I created but I'm not getting any error codes associated with my lines of code. (i.e. line 47 missing declaration before ';' ect..)

Here's what my code looks like.

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
#include<iomanip>
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

// protoypes
int buildArray(string, int, int, int);
void printArray(string, int, int, int, int);
void sortArray(string, int, int, int, int);


int main()
{
	const int size = 25;
	string playerArray[size];
	int goalArray[size], assistArray[size], ratingArray[size], number;

	number = buildArray(playerArray[size], goalArray[size], assistArray[size], ratingArray[size]);
	printArray(playerArray[size], goalArray[size], assistArray[size], ratingArray[size], number);
}

// list of functions

int buildArray(string playerNames[], int goals[], int assists[], int ratings[])
{
	ifstream hockeyFile;
	int i = 0, num = 0;
	string name;
	
	hockeyFile.open( "hockey.txt" );

if ( hockeyFile.fail() )
   {
  	 cout << "The hockey.txt input file did not open";
  	 exit (-1);
   }
   
   while( hockeyFile )
   {
	hockeyFile >> name;
	playerNames[i] = name;
	
	hockeyFile >> num;
	goals[i] = num;
	
	hockeyFile >> num;
	assists[i] = num;
	
	hockeyFile >> num;
	ratings[i] = num;
	
	i++;
   }
 hockeyFile.close();

 return i;
}

void printArray( string playerNames[], int goals[], int assists[], int ratings[], int number)
{
cout << "Player" << setw(15) << "Goals" << setw(15) << "Assists" << setw(15) << "Points" << setw(15) << "Plus/Minus";

	for (int i = 0; i < 60; i++)
		cout << "-";
		
	for (int sub = 0; sub < number; sub++)
	{
		int points = goals[sub]+assists[sub];
		
		cout << playerNames[sub] << setw(15) << goals[sub] << setw(15) << assists[sub] << setw(15) << points << setw(15) << showpos << ratings[sub];
	}
}

void sortArray( string playerNames[], int goals[], int assists[], int ratings[], int number)
{
	
}


My very last function header sortArray() is not getting the same error as the ones above it, even though it is unfinished at this point. And when I leave my cursor over the prototypes listed at the top, Dev C++ is telling me that they are public voids or ints and it's showing me my headers down below.

This should be a pretty simple program but I'm having a hard time debugging this.

Do you guys know what I did wrong here?
The correct declarations for the functions you defined are
1
2
int buildArray(string[], int[], int[], int[]);
int printArray(string[], int[], int[], int[], int);

sortArray() is not causing an error because you're not calling it anywhere. Only function calls and function pointers produce undefined reference errors.
Last edited on
Ahh thanks Helios, that solved my problem.

But now I'm getting this error here, in main().

21 94 C:\Users\family\Documents\BlackHawksRoster.cpp [Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'std::string* {aka std::basic_string<char>*}' for argument '1' to 'int buildArray(std::string*, int*, int*, int*)'
22 93 C:\Users\family\Documents\BlackHawksRoster.cpp [Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'std::string* {aka std::basic_string<char>*}' for argument '1' to 'void printArray(std::string*, int*, int*, int*, int)'


From my understanding, this is telling me that I can not put a string into a string array because the basic string is only a character? I'm assuming I have to define or declare my strings further somehow but don't really know how, since I thought this was pre built into Dev.

Sorry for so many questions, this is my first programming class and the first time I've ran into some trouble since I started.
You need to call your functions like:
number = buildArray(playerArray[size], goalArray[size], assistArray[size], ratingArray[size]);
Not only you passing single variables instead of arrays, you are accessing out of bounds.
Last edited on
Thank you very much MiiNiPaa, I guess I don't need to set the size of the array since I have already declared the number of elements in my declaration.
I guess I don't need to set the size of the array since I have already declared the number of elements in my declaration.

You weren't setting the size of the array. You were specifying an element of the array with an invalid index, in a place where the compiler expected a pointer to an element.
Topic archived. No new replies allowed.