0
votes

header.h

#include <iostream>
#include <vector>
#include <ctime>
#include <string>

using namespace std;

//vector <Account> bankAccounts; this is taken out.

extern vector <Account> bankAccounts; //edited




struct Account {
    int accountNumber;
    string lastName;
    string firstName;
    double accountbalance;
};

void menu(int*);
void makeAccount(vector <Account>&);

cpp

#include "Header.h"

void menu(int*);
void makeAccount(vector <Account>&);
vector <Account> bankAccounts; //edited 


int main() {



int input = 0;
int *inputPtr = &input;


menu(inputPtr);

switch (input) {
case 1:
    makeAccount(bankAccounts);

     }
}

another cpp

#include "Header.h"

vector <Account> bankAccounts; edited;

void menu(int *inputPtr) {

    int select = 0;

    cout << "Welcome to MadeUp Banking. Select options below: \n";
    cout << "\t 1. Make new account. \n";
    cout << "\t 2. Display to an account. \n";
    cout << "\t 3. Deposit to an account. \n";
    cout << "\t 4. Withdraw from an account. \n";
    cout << "\t 5. Print account. \n";
    cout << "\t 6. Delete an account. \n";
    cout << "\t 7. Quit. \n";
    cout << "Selection: ";
    cin >> select;
    *inputPtr = select;

}



void makeAccount(vector <Account> bankAccounts) {
    //edited vector <Account> bankAccounts within makeAccount()

return;

}

When program is ran, the error gives:

main_file.obj : error LNK2005: "class std::vector > bankAccounts" (?bankAccounts@@3V?$vector@UAccount@@V?$allocator@UAccount@@@std@@@std@@A) already defined in function_file.obj 1>main_file.obj : error LNK2019: unresolved external symbol "void __cdecl makeAccount(class std::vector > &)" (?makeAccount@@YAXAAV?$vector@UAccount@@V?$allocator@UAccount@@@std@@@std@@@Z) referenced in function _main

How do I go about fixing this error? Sorry I'm a rookie coder, if more details are needed, then please tell me and I will edit accordingly. Thank you for the help in advance.

2
Where did you define your menu, or makeAccount functions? All I can see in your example are declarations. EDIT: if there's multiple files including this header, then vector <Account> bankAccounts; will be defined multiple times in all translation units. Consider using extern when declaring the global variable, and then, define it once in a single translation unit. - Algirdas Preidžius
sorry, I edited it accordingly, with one more cpp file. - zycoactivtheory
You promised void makeAccount(vector <Account>&) but didn't deliver; I can only see void makeAccount() - doctorlove
thanks for all the suggestions. I edited the code accordingly. unfortunately the same errors still show. The edited code itself might be wrong, I'm not entirely sure. - zycoactivtheory

2 Answers

0
votes
  1. The error message of the first error, which can be abridged to:

    main_file.obj : error LNK2005: std::vector<Account> bankAccounts already defined in function_file.obj

    The reason for this error is that included files are copy-pasted in place of #include. That way, the std::vector<Account> bankAccounts is defined in both files (and they are, completely separate objects!), so the linker complains because of the multiple-definitions.

    To solve this, in the header file, declare the global variable as extern std::vector<Account> bankAccounts, and then define it, in one of the .cpp files as std::vector<Account> bankAccounts. extern, only declares that there will be such a variable defined, at some point, and since you will be defining a global variable once, the linker won't see multiple definitions of said variable.

  2. The second error is just like it sounds: you declared (and tried to call a function with signature void makeAccount(vector <Account>&);, however, you defined a function with signature void makeAccount();, leaving the function void makeAccount(vector <Account>&); undefined. Fix the definition of a function to include the parameter, present in its declarations:

    void makeAccount(vector <Account>&) {
        return;
    }
    

Unrelated to your issues, but I felt that I should mention several more things:

  1. Don't use using namespace std;, especially in the header files, it is considered a bad practice.
  2. How is your code even reaching the linker stage? It shouldn't even compile, because Account is declared after the void makeAccount(vector <Account>&);, and at the point of such function declaration - struct Account is not known to the compiler.
0
votes

1. bankAccounts already defined in function_file.obj.

You should define bankAccounts in your cpp file. Because if you define it in header file, when you include your header in multiple cpp files, there would be multiple definition of backAccounts.

If you needs it in multiple cpp files, use extern to declare(not define) it in your header file:

extern vector <Account> bankAccounts;

And in one of your cpp file, define it as:

vector <Account> bankAccounts;

2. unresolved external symbol void makeAccount()

Definition of makeAccount() should be like:

void makeAccount(vector <Account>&)
{
   // do something
}

While you are defining it as void makeAccount(vector<Account>). Please notice the difference. In your declaration, parameter is reference to a vector, while in your definition, parameter is vector object.