0

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.

  • 1
    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 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 Mar 22 '17 at 16:03
  • sorry, I edited it accordingly, with one more cpp file. – zycoactivtheory Mar 22 '17 at 16:07
  • You promised `void makeAccount(vector &)` but didn't deliver; I can only see `void makeAccount()` – doctorlove Mar 22 '17 at 16:12
  • 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 Mar 22 '17 at 16:26

2 Answers2

0

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.

zhm
  • 3,513
  • 3
  • 34
  • 55
  • followed your advice & edited the code accordingly, but the program still shows the same errors. I might've edited it wrong – zycoactivtheory Mar 22 '17 at 16:25
  • @zycoactivtheory Both two errors still exist? Or there are different errors other than the ones in your question? – zhm Mar 22 '17 at 16:28
  • @zycoactivtheory Please edit your question with your modified code, so we can know what is going wrong. – zhm Mar 22 '17 at 16:30
  • @zycoactivtheory You should define `vector bankAccounts;` in **one of** your cpp file, not both. – zhm Mar 22 '17 at 16:32
  • the code was modified, and it is within the original post. I indicated where I edited using //edited and such. EDIT: ok will try that now. – zycoactivtheory Mar 22 '17 at 16:34
  • @zycoactivtheory And for another issue, `vector &` is not equal to `vector`. First is reference, second is object. Parameter in your definition of `makeAccount()` should be reference (same with parameter type in your declaration). – zhm Mar 22 '17 at 16:34
  • OK! The program runs successfully. Thanks for the help Martin! Let me see if the code works as intended though. – zycoactivtheory Mar 22 '17 at 16:36
0
  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.
Community
  • 1
  • 1
Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17
  • thanks for the suggestions. followed your advice & edited the code accordingly, but the program still shows the same errors. I might've edited it wrong however. As for the using namespace std; I will drop that habit as soon as my university course is finished. The professor recommends students to use it at least for her course. – zycoactivtheory Mar 22 '17 at 16:27
  • @zycoactivtheory Did you recompile your source files, before trying to link `.obj` files together? – Algirdas Preidžius Mar 22 '17 at 16:28
  • sorry! some clarification please.... I'm using Visual Studios, and as a novice, I have no idea what it means to link .obj files together. I just build & run (also by the command ctrl + f5) – zycoactivtheory Mar 22 '17 at 16:32
  • 1
    @zycoactivtheory 1) Please don't try to fix your code in the question, because, then, it becomes useless in long-term research. 2) You read my answer wrong. I even emphasized, that you need to define it in **one** of the .cpp files, but you, once again defined it in both. 3) `vector &` is not the same as `vector `, meaning - you still didn't provide a definition for `void makeAccount(vector &);`. – Algirdas Preidžius Mar 22 '17 at 16:32
  • 1) I left the original code in there and just added some comments & additional lines of code to indicate where it was modified. Would that still cause an issue with long-term research? 2) Yes, sorry about that. I fixed it accordingly in my program. 3) Yes, thank you, I also just fixed that. The gentleman below also pointed these out. The program runs successfully now. The only thing left is to test out its functionality and see if it works as intended. , really appreciate the help Algirdas! – zycoactivtheory Mar 22 '17 at 16:41