本文介绍了奇怪的多重定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! vio @!@#$:〜/ cpp / OOP / 6 $ g ++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.o AccountHandler.o :( .bss + 0x0):`AccountHandler :: account_number'的多重定义 main.o :( .bss + 0x0):首先定义这里 collect2:ld返回1退出状态 我收到上面的错误信息。 但我找不到代码,它是多重定义,所以我将所有account_number改为number_of_account在account.h和AccountHandler.cpp和 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):多重定义`AccountHandler :: number_of_account' main.o :( .bss + 0x0):首先定义 collect2:ld返回1退出状态 ,并再次出现错误消息。 我在所有头文件中使用了#ifndef #define #define,当我更改AccountHandler.cpp和accounthandler.h中的变量时,使用,它再次编译得很好, #ifndef __ACCOUNTHANDLER_H__ #define __ACCOUNTHANDLER_H__ #includeaccount.h class AccountHandler { private:帐户*帐户[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 解决方案你在头中定义一些东西,然后它将在每个包含该头的翻译单元中定义 - 在你的情况下, AccountHandler 和 main 。您应该在标题中声明(如果需要从多个单元访问),并在一个源文件中定义。 假设它是一个静态类成员(我不得不猜测,因为你忘了给我们代码),你想要的东西: // header class AccountHandler { public: static size_t number_of_account; // declaration //其他成员... }; //源文件 size_t AccountHandler :: number_of_account; //定义 可能在您的代码中,该定义在标题中。 这假设它应该是静态的;它独立于任何特定帐户(例如,其代表存在的帐户的数量),而不是与每个帐户相关联(例如它代表帐号)。如果它不应该是静态的,那么确保它没有声明 static 。 包括守卫不会帮助有了这个;它们防止标题在每个翻译单元中被包括多于一次,但仍然允许它们被包括在多个单元中。 vio@!@#$:~/cpp/OOP/6$ g++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.oAccountHandler.o:(.bss+0x0): multiple definition of `AccountHandler::account_number'main.o:(.bss+0x0): first defined herecollect2: ld returned 1 exit statusI 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.cppvio@!@#$:~/cpp/OOP/6$ g++ -c AccountHandler.cppvio@!@#$:~/cpp/OOP/6$ g++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.ovio@!@#$:~/cpp/OOP/6$it compiled well.After that, I changed main.cpp a little vio@!@#$:~/cpp/OOP/6$ g++ -c main.cppvio@!@#$:~/cpp/OOP/6$ g++ -o main main.o NormalAccount.o HighCreditAccount.o Account.o AccountHandler.oAccountHandler.o:(.bss+0x0): multiple definition of `AccountHandler::number_of_account'main.o:(.bss+0x0): first defined herecollect2: ld returned 1 exit statusand error message emerged again.I used #ifndef #define #define in all header file andwhen I changed the variable in AccountHandler.cpp and accounthandler.h, it compiled well again,so I wonder why it happensHere 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 解决方案 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:// headerclass AccountHandler{public: static size_t number_of_account; // declaration // other members...};// source filesize_t AccountHandler::number_of_account; // definitionPresumably, 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. 这篇关于奇怪的多重定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 16:07