Question: C++: Undefined Reference / Linkage Error? I'm using three files - main.cpp, function_file.cpp, header_file.h. When including the header file in each of the .cpp files,
C++: Undefined Reference / Linkage Error?
I'm using three files - main.cpp, function_file.cpp, header_file.h. When including the header file in each of the .cpp files, I'm getting an error at each of the function calls in main that...
||=== Build: Debug in PMProject1 (compiler: GNU GCC Compiler) ===| obj\Debug\main.o||In function `main':| C:\Users\x\Desktop\PM Project 1\main.cpp|39|undefined reference to `void makeAccount(std::vector >&)'| C:\Users\x\Desktop\PM Project 1\main.cpp|43|undefined reference to `void PrintAllAccount(std::vector >&)'| C:\Users\x\Desktop\PM Project 1\main.cpp|47|undefined reference to `void depositAccount(std::vector >&)'| C:\Users\x\Desktop\PM Project 1\main.cpp|51|undefined reference to `void withdrawAccount(std::vector >&)'| C:\Users\x\Desktop\PM Project 1\main.cpp|55|undefined reference to `void printAccount(std::vector >&)'| C:\Users\x\Desktop\PM Project 1\main.cpp|59|undefined reference to `void deleteAccount(std::vector >&)'| ||error: ld returned 1 exit status| ||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I'm using code::blocks and all files are correctly added to the project.
Here are the relevant files:
Main:
#include "header_file.h" #include #include #include #include
using namespace std;
int main() { // declare list of account here. The size of the list is not fixed.
struct Account{ int accountNumber; string lastName; string firstName; double accountBalance; };
vector bankAccounts; Account a;
int input = 0; int *inputPtr = &input; while(input != 7) { menu(inputPtr); // run loop to continue program until terminated by user
switch (input) { //cases: call functions to perform tasks case 1: makeAccount(bankAccounts); break;
case 2: PrintAllAccount(bankAccounts); break;
case 3: depositAccount(bankAccounts); break;
case 4: withdrawAccount(bankAccounts); break;
case 5: printAccount(bankAccounts); break;
case 6: deleteAccount(bankAccounts); break;
case 7: return 0; break;
default: cout << "Invalid Input. Choose a number from 1 through 7." << endl << endl; } } return 0; }
-------
beginning of the function_file.cpp:
// function_file.cpp #include #include #include #include #include #include #include #include "header_file.h"
int index; using namespace std;
void menu(int *num) { int select = 0; cout << "Welcome to Made_up Banking. Select options below:" << endl; cout << "\t1. Make new account." << " \t2. Display all accounts." << " \t3. Deposit to an account." << " \t4. Withdraw from an account." << " \t5. Print account." << " \t6. Delete an Account." << " \t7. Quit." << endl; cout << "Selection:\t"; cin >> select; *num = select; }
template void makeAccount(vector & newA) {
typeStruct newAccount;
srand(time(0)); newAccount.accountNumber = (rand() % 9999) + 1; cout << " Creating bank account number: " << newAccount.accountNumber << endl;
cout << "Enter first name:" << endl; cin >> newAccount.firstName;
cout << endl << "Enter last name:" << endl; cin >> newAccount.lastName;
cout << endl << "Enter starting balance:" << endl; cin >> newAccount.accountBalance;
newA.push_back(newAccount); cout << "Your Account has been created! " << endl;
//sortAccounts();
} //more functions below in this file
-------
header_file:
// header_file.h
#ifndef H_GLOBALS #define H_GLOABLS
#include #include #include #include #include
using namespace std;
void menu(int*);
template void makeAccount(vector &);
template void makeAccount(vector &);
template void printAccount(vector &);
template void PrintAllAccount(vector &);
template void depositAccount(vector &);
template void withdrawAccount(vector &);
template void sortAccounts(vector &); // sort the accounts using the account numbers
template void deleteAccount(vector &);
template int validInput(int, vector &);
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
