Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板化代码的LNK2005和LNK1169错误_C++_Templates_Linker Errors_Lnk2005 - Fatal编程技术网

C++ 模板化代码的LNK2005和LNK1169错误

C++ 模板化代码的LNK2005和LNK1169错误,c++,templates,linker-errors,lnk2005,C++,Templates,Linker Errors,Lnk2005,请先阅读我的具体问题,然后再将此问题标记为重复问题,因为没有其他问题可以回答我的问题 我收到以下链接错误: 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和头

请先阅读我的具体问题,然后再将此问题标记为重复问题,因为没有其他问题可以回答我的问题

我收到以下链接错误:

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和头文件\u.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
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;
            }
}
#包括“header_file.h”
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用std::endl;
使用std::vector;
使用std::string;
int菜单()
{
int-choice=0;

无法搜索“c++模板头文件”的堆栈溢出你的问题可能是一个副本。可能的副本:我需要做什么改变?我不熟悉模板,我不理解。你不熟悉模板,但你正在使用它们。将模板移动到一个头文件,然后将头文件包含到源文件中。考虑模板作为模板。模板不生成。代码,除非使用数据类型调用。如我上面的代码所示,我在头文件中有模板,并将头文件包含在main.cpp和function.cpp中,但仍然收到此错误
#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