0

Please read my specific problem before you mark this question as a duplicate because no other questions answer my problem.

I am getting the following link errors:

error LNK2005: "int__cdecl menu(void)" (?menu@@YAHXZ) already defined in function.obj" 
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.

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 << "\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
valiano
  • 16,433
  • 7
  • 64
  • 79
python_
  • 11
  • 3
  • 1
    Search StackOverflow for "c++ template header file". Your issue may be a duplicate. – Thomas Matthews Mar 24 '17 at 18:00
  • Possible duplicate: [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Thomas Matthews Mar 24 '17 at 18:01
  • what changes do I need to make? I'm not familiar with templates – python_ Mar 24 '17 at 18:09
  • I don't understand. You are unfamiliar with templates, yet you are using them. Move the templates to a header file, then include the header file into your source file. Consider a template as a stencil. A template doesn't generate code unless invoked with a data type. – Thomas Matthews Mar 24 '17 at 19:51
  • As seen in my above code, I have the templates in a header file and included the header file in the main.cpp and function.cpp and am still getting this error – python_ Mar 26 '17 at 22:10

0 Answers0