请在将此问题标记为重复之前阅读我的具体问题,因为没有其他问题可以解答我的问题。
我收到以下链接错误:
error LNK2005: "int__cdecl menu(void)" (?menu@@YAHXZ) already defined in function.obj"
error LNK1169: one or more multiply defined symboles found
我理解错误是什么,但我无法弄清楚如何解决它们。
我有一个function.cpp,main.cpp和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 << "\n\nWelcome to MadeUp Banking. Select options below:" << endl;
cout <<"\t 1. Make new account." << endl;
cout <<"\t 2. Display all accounts." << endl;
cout <<"\t 3. Deposit to an account." << endl;
cout <<"\t 4. Withdraw from an account." << endl;
cout <<"\t 5. Print account." << endl;
cout <<"\t 6. Delete an account." << endl;
cout <<"\t 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 << "\nCreating bank account number " << accNum << endl;
typeStruct account;
account.accountNumber = accNum;
cout << "\nEnter 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 << "\nAccount number: " << myVec[i].accountNumber << "\t";
cout << "\t\tBalance: " << std::fixed << std::setprecision(2) << myVec[i].accountBalance << endl;
cout << "\t\tLast name: " << myVec[i].lastName << "\t\t" << "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 << "\t";
cout << "Balance: " << std::fixed << std::setprecision(2) << myVec[i].accountBalance << endl;
cout << "Last name: " << myVec[i].lastName << "\t" << "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