Strange multiple definition error

2019-09-06 19:35发布

问题:

vio@!@#$:~/cpp/OOP/6$ g++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.o
AccountHandler.o:(.bss+0x0): multiple definition of `AccountHandler::account_number'
main.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

I got the error message above. But I couldn't find the code where it is multiply defined, so I changed all account_number to number_of_account in 'account.h' and 'AccountHandler.cpp' and

vio@!@#$:~/cpp/OOP/6$ vi AccountHandler.cpp 
vio@!@#$:~/cpp/OOP/6$ g++ -c AccountHandler.cpp 
vio@!@#$:~/cpp/OOP/6$ g++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.o
vio@!@#$:~/cpp/OOP/6$

it compiled well.

After that, I changed main.cpp a little

vio@!@#$:~/cpp/OOP/6$ g++ -c main.cpp
vio@!@#$:~/cpp/OOP/6$ g++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.o
AccountHandler.o:(.bss+0x0): multiple definition of `AccountHandler::number_of_account'
main.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

and error message emerged again.

I used #ifndef #define #define in all header file and when I changed the variable in AccountHandler.cpp and accounthandler.h, it compiled well again, so I wonder why it happens

Here is the code:

#ifndef __ACCOUNTHANDLER_H__
#define __ACCOUNTHANDLER_H__

#include "account.h"

class AccountHandler
{
private:
    Account* account[100];
    static int number_of_account;
public:
    AccountHandler(){}

    void show_menu();
    void make_account();
    void deposit_money();
    void withdraw_money();
    void show_all_account_info();

    ~AccountHandler();
};

int AccountHandler::number_of_account=0;

#endif

回答1:

If you define something in a header, then it will be defined in every translation unit that includes that header - in your case, both AccountHandler and main. You should declare it in a header (if it needs to be accessed from multiple units), and define it in just one source file.

Assuming that it's a static class member (I'm having to guess, since you forgot to show us the code), you want something like:

// header
class AccountHandler 
{
public:
    static size_t number_of_account; // declaration

    // other members...
};

// source file
size_t AccountHandler::number_of_account; // definition

Presumably, in your code, that definition is in the header.

That's assuming it's supposed to be static at all; that it's independent of any particular account (e.g. it represents the number of accounts that exist), rather than being associated with each account (e.g. it represents an account number). If it's not supposed to be static, then make sure it's not declared static.

Include guards won't help with this; they prevent the header from being included more than once in each translation unit, but still allow them to be included from multiple units.