I am getting \"error LNK2005: \"int__cdecl menu(void)\" (?menu@@YAHXZ) already d
ID: 3803912 • Letter: I
Question
I am getting "error LNK2005: "int__cdecl menu(void)" (?menu@@YAHXZ) already defined in function.obj" and "error LNK1169: one or more multiply defined symboles found"
I understand what the errors are, but I can't figure out how to fix them.Please help fix them
I have a function.cpp, main.cpp, and header_file.h:
function.cpp:
#include "header_file.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int menu()
{
int choice = 0;
cout << " Welcome to MadeUp Banking. Select options below:" << endl;
cout <<" 1. Make new account." << endl;
cout <<" 2. Display all accounts." << endl;
cout <<" 3. Deposit to an account." << endl;
cout <<" 4. Withdraw from an account." << endl;
cout <<" 5. Print account." << endl;
cout <<" 6. Delete an account." << endl;
cout <<" 7. Quit." << endl;
cout <<"Selection: ";
cin >> choice;
while(choice < 1 || choice > 7)
{
cout << "Invalid choice. Enter a valid choice: ";
cin >> choice;
}
return choice;
}
template <typename typeStruct>
void makeAccount(vector <typeStruct>& myVec)
{
bool duplicate = true;
int accNum;
string fName, lName;
double balance;
while(duplicate)
{
duplicate = false;
accNum = rand() % 9000 + 1000;
for(int i = 0; i < myVec.size(); i++)
if(myVec[i].accountNumber == accNum)
duplicate = true;
if(!duplicate)
break;
}
cout << " Creating bank account number " << accNum << endl;
typeStruct account;
account.accountNumber = accNum;
cout << " Enter first name: ";
cin >> fName;
account.firstName = fName;
cout << "Enter last name: ";
cin >> lName;
account.lastName = lName;
cout << "Enter starting balance: ";
cin >> balance;
account.accountBalance = balance;
myVec.push_back(account);
}
template <typename typeStruct>
void printAllAccounts(vector <typeStruct> myVec)
{
for(int i = 0; i < myVec.size(); i++)
{
cout << " Account number: " << myVec[i].accountNumber << " ";
cout << " Balance: " << std::fixed << std::setprecision(2) << myVec[i].accountBalance << endl;
cout << " Last name: " << myVec[i].lastName << " " << "First name: " << myVec[i].firstName << endl;
}
}
template <typename typeStruct>
void printAccount(vector <typeStruct> myVec)
{
int accNum;
cout << "Enter account number to print: ";
cin >> accNum;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
cout << "Account number: " << myVec[i].accountNumber << " ";
cout << "Balance: " << std::fixed << std::setprecision(2) << myVec[i].accountBalance << endl;
cout << "Last name: " << myVec[i].lastName << " " << "First name: " << myVec[i].firstName << endl;
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void depositAccount(vector <typeStruct>&myVec)
{
int accNum;
double amount;
cout << "Enter account number for deposit: ";
cin >> accNum;
cout << "Enter amount to be deposited: ";
cin >> amount;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
myVec[i].accountBalance += amount;
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void withdrawAccount(vector <typeStruct>& myVec)
{
int accNum;
double amount;
cout << "Enter account number for withdraw: ";
cin >> accNum;
cout << "Enter amount to be withdrawn: ";
cin >> amount;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
myVec[i].accountBalance -= amount;
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void deleteAccount(vector <typeStruct>& myVec)
{
int accNum;
double amount;
cout << "Enter account number to be deleted: ";
cin >> accNum;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
myVec.erase(myVec.begin() + i);
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void sortAccounts(vector <typeStruct>& bankAccounts)
{
for(int i = 0; i < bankAccounts.size()-1; i++)
for(int j = 0; j < bankAccounts.size()-i-1; j++)
if(bankAccounts[j].accountNumber > bankAccounts[j+1].accountNumber)
{
typeStruct temp = bankAccounts[j];
bankAccounts[j] = bankAccounts[j+1];
bankAccounts[j+1] = temp;
}
}
------------------------------------------------------------------------------------------------------------------------------------
main.cpp"
#include "function.cpp"
#include "header_file.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
std::cout << std::flush;
vector<Account> accounts;
while(true)
{
int choice = menu();
switch(choice)
{
case 1: makeAccount(accounts); break;
case 2: printAllAccounts(accounts); break;
case 3: depositAccount(accounts); break;
case 4: withdrawAccount(accounts); break;
case 5: printAccount(accounts); break;
case 6: deleteAccount(accounts); break;
case 7: return 0;
default: cout << "Invalid menu..." << endl;
}
}
}
-----------------------------------------------------------------------------------------------------------
header_file.h:
#ifndef MYNODE_H_
#define MYNODE_H_
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
typedef struct Account{
int accountNumber;
string lastName;
string firstName;
double accountBalance;
}Account;
int menu();
template <typename typeStruct>
void makeAccount(vector <typeStruct>&);
template <typename typeStruct>
void printAllAccounts(vector <typeStruct>);
template <typename typeStruct>
void printAccount(vector <typeStruct>);
template <typename typeStruct>
void depositAccount(vector <typeStruct>&);
template <typename typeStruct>
void withdrawAccount(vector <typeStruct>&);
template <typename typeStruct>
void deleteAccount(vector <typeStruct>&);
template <typename typeStruct>
void sortAccounts(vector <typeStruct>&);
#endif
Explanation / Answer
Here is the working code for you:
Banking.h:
#include <iostream>
#include <vector>
using namespace std;
typedef struct Account{
int accountNumber;
string lastName;
string firstName;
double accountBalance;
}Account;
int menu();
template <typename typeStruct>
void makeAccount(vector <typeStruct>&);
template <typename typeStruct>
void printAllAccounts(vector <typeStruct>);
template <typename typeStruct>
void printAccount(vector <typeStruct>);
template <typename typeStruct>
void depositAccount(vector <typeStruct>&);
template <typename typeStruct>
void withdrawAccount(vector <typeStruct>&);
template <typename typeStruct>
void deleteAccount(vector <typeStruct>&);
template <typename typeStruct>
void sortAccounts(vector <typeStruct>&);
Banking.cpp:
#include "Banking.h"
#include <iomanip>
int menu()
{
int choice;
cout << "Welcome to MadeUp Banking. Select options below:" << endl;
cout <<" 1. Make new account." << endl;
cout <<" 2. Display all accounts." << endl;
cout <<" 3. Deposit to an account." << endl;
cout <<" 4. Withdraw from an account." << endl;
cout <<" 5. Print account." << endl;
cout <<" 6. Delete an account." << endl;
cout <<" 7. Quit." << endl;
cout <<" Selection: ";
cin >> choice;
while(choice < 1 || choice > 7)
{
cout << "Invalid choice. Enter a valid choice: ";
cin >> choice;
}
return choice;
}
template <typename typeStruct>
void makeAccount(vector <typeStruct>& myVec)
{
bool duplicate = true;
int accNum;
string fName, lName;
double balance;
while(duplicate)
{
duplicate = false;
accNum = rand() % 9000 + 1000;
for(int i = 0; i < myVec.size(); i++)
if(myVec[i].accountNumber == accNum)
duplicate = true;
if(!duplicate)
break;
}
cout << "Creating bank account number " << accNum << endl;
typeStruct account;
account.accountNumber = accNum;
cout << "Enter first name: ";
cin >> fName;
account.firstName = fName;
cout << "Enter last name: ";
cin >> lName;
account.lastName = lName;
cout << "Enter starting balance: ";
cin >> balance;
account.accountBalance = balance;
myVec.push_back(account);
}
template <typename typeStruct>
void printAllAccounts(vector <typeStruct> myVec)
{
for(int i = 0; i < myVec.size(); i++)
{
cout << "Account number: " << myVec[i].accountNumber << " ";
cout << "Balance: " << fixed << setprecision(2) << myVec[i].accountBalance << endl;
cout << "Last name: " << myVec[i].lastName << " " << "First name: " << myVec[i].firstName << endl;
}
}
template <typename typeStruct>
void printAccount(vector <typeStruct> myVec)
{
int accNum;
cout << "Enter the account number: ";
cin >> accNum;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
cout << "Account number: " << myVec[i].accountNumber << " ";
cout << "Balance: " << fixed << setprecision(2) << myVec[i].accountBalance << endl;
cout << "Last name: " << myVec[i].lastName << " " << "First name: " << myVec[i].firstName << endl;
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void depositAccount(vector <typeStruct>&myVec)
{
int accNum;
double amount;
cout << "Enter the account number to deposit: ";
cin >> accNum;
cout << "Enter amount to be deposited: ";
cin >> amount;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
myVec[i].accountBalance += amount;
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void withdrawAccount(vector <typeStruct>& myVec)
{
int accNum;
double amount;
cout << "Enter the account number to withdraw: ";
cin >> accNum;
cout << "Enter amount to be withdrawn: ";
cin >> amount;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
myVec[i].accountBalance -= amount;
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void deleteAccount(vector <typeStruct>& myVec)
{
int accNum;
double amount;
cout << "Enter the account number to delete: ";
cin >> accNum;
bool found = false;
for(int i = 0; i < myVec.size(); i++)
{
if(myVec[i].accountNumber == accNum)
{
myVec.erase(myVec.begin() + i);
found = true;
break;
}
}
if(!found)
cout << "Account number doesn't exist..." << endl;
}
template <typename typeStruct>
void sortAccounts(vector <typeStruct>& bankAccounts)
{
for(int i = 0; i < bankAccounts.size()-1; i++)
for(int j = 0; j < bankAccounts.size()-i-1; j++)
if(bankAccounts[j].accountNumber > bankAccounts[j+1].accountNumber)
{
typeStruct temp = bankAccounts[j];
bankAccounts[j] = bankAccounts[j+1];
bankAccounts[j+1] = temp;
}
}
/*void sortAccounts()
{
for(int i = 0; i < bankAccounts.size()-1; i++)
for(int j = 0; j < bankAccount.size()-i-1; j++)
if(bankAccounts[j].accountNumber > bankAccounts[j+1].accountNumber)
{
Account temp = bankAccounts[j];
bankAccounts[j] = bankAccounts[j+1];
bankAccounts[j+1] = temp;
}
}*/
And for BankingDemo.cpp:
#include "Banking.cpp"
int main()
{
vector<Account> accounts;
while(true)
{
int choice = menu();
switch(choice)
{
case 1: makeAccount(accounts); break;
case 2: printAllAccounts(accounts); break;
case 3: depositAccount(accounts); break;
case 4: withdrawAccount(accounts); break;
case 5: printAccount(accounts); break;
case 6: deleteAccount(accounts); break;
case 7: return 0;
default: cout << "Invalid menu..." << endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.