Is there a way around this? LNK2005 already defined in xx.obj

I have 4 files:

bank.h includes function definitions and structs.
bank.cpp includes main()and function code.
customer.h class for customer
customer.cpp implementation filefor customer class

The functions used customer.cpp need the structs from bank.h
I include the bank.h in the bank.cpp and customer.cpp and get the error:

error LNK2005: "struct customer * customerRecords" already defined in bank.obj
error LNK2005: "struct logs * dailyLogs" already defined in bank.obj

I know why I am getting the error sincee Iam using #include bank.h twice, but I am not sure how to solve this problem. I need the structs for both files. Any help would be great. thanks
You need header guards. They will prevent redefinition of your header files. Also, you shouldn't be including bank from customer since customers don't need the bank information (or at least they shouldn't in my mind).
closed account (zb0S216C)
So far, I can think of these reasons:

1) You haven't got header guards
2) You're #including source files
3) Your object files need to be replaced with new ones (delete the old ones)
4) A function is being redefined elsewhere

Do any of these look familiar?

Wazzak
Also, you shouldn't be including bank from customer since customers don't need the bank information (or at least they shouldn't in my mind).

I agree. I am converting a program I wrote a while ago to use classes, and am just trying to get it working. This is my first real issue so far.


1) You haven't got header guards
2) You're #including source files
3) Your object files need to be replaced with new ones (delete the old ones)
4) A function is being redefined elsewhere


I have header guards in place

I am not including any .cpp files

I tried deleting the old .obj files.

The functions for customer are stated in the customer.h class definition and then defined the the customer.cpp file. If I put the customer function definitions into the bank.cpp and get rid of customer.cpp it works fine. The problem is including bank.h in the customer.cpp file and the bank.cpp. Looking for a workaround.
If you're using an IDE make sure your project has both .cpp files in it and that they're both linked together (typically done "behind the scenes" with the project). It might also be referring to you redefining the structs customer and log. Make sure you only have one instance of both.
I'd venture a guess that they are doing 1. I need to be better at doing that, of course I had never heard of that until I read D.S. Malik's book.

That is if you're meaning:
1
2
3
4
#ifdef HEADERNAME_H
#define HEADERNAME_H
///header code
#endif 
If you need a shared variable, use extern. Define it only one time in just one source file.
Function definitions don't go in header, except for inline and template
If you're using an IDE make sure your project has both .cpp files in it and that they're both linked together (typically done "behind the scenes" with the project). It might also be referring to you redefining the structs customer and log. Make sure you only have one instance of both.


Using Visual Studio 2010 and both files are there. The structs are called in the customer.cpp for the customer class functions and they are both called in the bank.cpp for various other functions. Is there a way to give both .cpp files access to the structs without calling the same header file? These structs are used everywhere in the code. I am trying to split out groups of functions into classes. I would like to use .h and a .cpp implimentation file for the classes, but if I can't give everyone access to the structs I may have to scrap the implimentation files and put it all in bank.cpp.
So you have a line like this in two different places?
struct customer * customerRecords;

This is a big no no, you can't have two variables with the same names, and you definitely don't need to specify the variables are a "struct" customer, simply customer * customerRecords; is enough. That line creates a customer pointer named customerRecords that points to nothing (I believe it defaults to NULL). If you're trying to declare a struct like that, I'm not 100% what the reason would be, but it's also not something I understand.

Maybe posting your code here would allow us to help you better. If it's extremely long, use pastebin or something similar and paste the urls here so we can view it.
Sorry, it is actually an array of structs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct customer {
    int accountNumber;
    string firstName;
    string lastName;
    float accounts[4] ;
};

customer customerRecords [CUSTOMER_CAPACITY];

struct logs {
    int accNum;
    string fName;
    string lName;
    char transType;
    float ammount; 
    int accType;
};

logs dailyLogs[LOG_CAPACITY];


This declaration only exists in bank.h
Can you paste the entire files? Maybe you aren't properly implementing the header guards or it's possible that in bank.cpp or bank.h you're using the variables incorrectly.
I'll repeat
1
2
3
4
//bank.h (properly guarded)
//these are shared variables (declaration)
extern customer customerRecords [CUSTOMER_CAPACITY];
extern logs dailyLogs[LOG_CAPACITY];

1
2
3
4
//customer.cpp (or the one that you considerer proper
#include "bank.h"
//define the variable (only here, no other source may have this line
customer customerRecords [CUSTOMER_CAPACITY]; 


However, ¿why do you need it to be global?
code
Last edited on
code
Last edited on
Some of the formatting got mixed up in the copy/pasta.

I'll repeat

//bank.h (properly guarded)
//these are shared variables (declaration)
extern customer customerRecords [CUSTOMER_CAPACITY];
extern logs dailyLogs[LOG_CAPACITY];

//customer.cpp (or the one that you considerer proper
#include "bank.h"
//define the variable (only here, no other source may have this line
customer customerRecords [CUSTOMER_CAPACITY];

However, ¿why do you need it to be global?


I am not familiar with extern. I tried adding it to those 2 declarations and it gave me "atal error LNK1120: 2 unresolved externals".

As for the second part the variables for the array of structs are only declared in the bank.h file. It needs to be global because there are functions in the bank.cpp and customer.cpp that use the array of structs.
I don't believe the customer class needs to use logs or an array of itself so those arrays don't need to be global.

@customer.cpp
1
2
#include "customer.h"
#include "bank.h" // <--- WHY?!?!?!?!??!?!?!? 


bank.h is your banking system, why does the customer class need to use anything in the banking system? I believe you need to really rethink your classes.

A customer should have a name, possible accounts, and maybe a length of how long they've been there (in years). I believe you should have an account class as well (separate from the other two classes) that should hold an account number, a balance, and have a few functions that allow withdraw and deposits. Your bank class should contain a way to number each account, link each account to each other, erase whatever accounts are closed, and create new accounts (and a new customer if nessecary).

As a side note, an account should be your basic level. A customer can have accounts so it would need to include that, and since a bank has customers (that have accounts) it needs to include customers. It's also typically a very bad idea to include your method definitions in the "main" part of your program. That's why it's most common to name whatever file contains the function main as main.cpp.
Ok, #include "bank.h" is there because the array of structs is referenced in all the functions for the customer. If I remove it I get a series of the following errors.

customer.cpp(30): error C2065: 'customerRecords' : undeclared identifier
customer.cpp(30): error C2228: left of '.accounts' must have class/struct/union
...

Thanks for the tips. If I can't get this working I may just scrap it and start over.
If you need a shared variable, use extern. Define it only one time in just one source file.
Function definitions don't go in header, except for inline and template


I did some reading on extern and somehow missed your first post. This fixed the issue. thank you.
Topic archived. No new replies allowed.