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
我收到了上面的错误消息。但是我找不到它被多重定义的代码,所以我在'account.h'和'AccountHandler.cpp'中将所有account_number更改为number_of_account,并且
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$
它编译得很好。
之后,我稍微改变了 main.cpp
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
并再次出现错误消息。
我在所有头文件中使用了#ifndef #define #define,当我更改AccountHandler.cpp和accounthandler.h中的变量时,它再次编译得很好,所以我想知道为什么会这样
这是代码:
#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