Linker Error: Unidentified Reference to...

I'm currently in the process of modifying a program for class but I'm getting a linker error.

[Linker error] undefined reference to `print_accts(BankAccount*, int, std::basic_ofstream<char, std::char_traits<char> >&)'
ld returned 1 exit status

The part of the code that I'm working on modifying is

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
#include <iostream>
#include <fstream>
using namespace std;

const int maxaccs=50;

class Name {
      public:
      string first;
      string last;
};

class Depositor {
      public:
      Name firstlast;
      int socialsecurity;
};

class BankAccount {
      public:
      Depositor depositor;
      int account;
      string type;
      double balance;
};

int read_accts (BankAccount [], ofstream &);
//int findacct (int [], int);
//void menu (int [], double [], int, ofstream &);
//void Balance (int [], double [], int, ofstream &);
//void withdraw (int [], double [], int, ofstream &);
//void deposit (int [], double [], int, ofstream &);
//int new_acct (int [], double [], int, ofstream &);
void print_accts (BankAccount [], int, ofstream &);
//int delete_acct (int [], double [], int, ofstream &);

int main ()
{
    //ofstream outfile ("C:\\transaction.txt");
    ofstream outfile ("con");
    //int accs[maxaccs], accnum;
    //double balance[maxaccs];
    int accnum;
    BankAccount bankaccount[maxaccs];

    accnum = read_accts(bankaccount, outfile);
    print_accts (bankaccount, accnum, outfile);
    //menu (accs, balance, accnum, outfile);

    system("PAUSE");
    return 0;
}
/*
Input - acctnum - used to print account #
      - balance - used to print balance
      - num_accts - total amount of accounts
Process - goes into a loop that continues
        - until all accounts are printed
Output - prints account number and balance
*/
void print_accts (const BankAccount bankaccount [], int accnum, ofstream &out)
{
     int n = 0;
     while (n < accnum)
     {
           out<<"Name: "<<bankaccount[n].depositor.firstlast.first<<endl;
           //out<<"Account Number: "<<
          // n = n + 1;
     }
     return;
}

/*
Input - acctnum - array to hold all account numbers
      - balance - array to hold all balance
Process - goes through a loop that reads a file
        - until the end of file
Output - prints the account # and balance
*/
int read_accts(BankAccount bankaccount [], ofstream &out)
{
    ifstream cin("C:\\c++ data\\hw#7 data.txt");
    int num_accts = 0;
    while(!cin.eof())
       {
          cin>>bankaccount[num_accts].depositor.firstlast.first;
          cin>>bankaccount[num_accts].depositor.firstlast.last;
          cin>>bankaccount[num_accts].depositor.socialsecurity;
          cin>>bankaccount[num_accts].account;
          cin>>bankaccount[num_accts].type;
          cin>>bankaccount[num_accts].balance;
          /*cin>>acctnum[num_accts]>>balance[num_accts];
          out<<"Account #: "<<acctnum[num_accts]<<endl;
          out<<"Balance : $"<<balance[num_accts]<<endl<<endl;*/
          num_accts++;
          cout<<endl;
       }
       cin.close();
    return num_accts;
}


I tried posting the entire code even with the ones I'm not using but there was a limit on the amount of characters, I'll try posting it again if its needed.

edit---

It seems the const in my print_accts function is giving the error. Does anyone know why?

edit2---

Forgot that I had to include const in the function prototype as well. Problem solved.
Last edited on
When using pre-declarations, make sure your function signature is an exact match.

1
2
void print_accts (BankAccount [], int, ofstream &);
void print_accts (const BankAccount bankaccount [], int accnum, ofstream &out) { ... }
Last edited on
Topic archived. No new replies allowed.