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).
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
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.
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.
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.
//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];
//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.
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.