MESSAGE
DATE | 2015-02-20 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] a little C++ insite
|
Its been a while since I coded and I forget things... so I picked this nugget out of irc this morning and thought I would share it.
* otto_otto has quit (Ping timeout: 246 seconds) I have bnode *root; in my .h file * lleo (58d38391-at-gateway/web/freenode/ip.88.211.131.145) has left ##c++ and the compiler is complaining * japro (~jprogsch-at-212.55.208.122) has joined ##c++ "multiple definition of..." ? btree_main.o:(.bss+0x0): multiple definition of `root' exactly :) * Raziel (~Raziel-at-cpc16-broo8-2-0-cust158.14-2.cable.virginm.net) has joined ##c++ So obviously I'm an idiot * Gvidon has quit (Quit: Leaving.) the short term answer is "extern", the long term answer is "don't use globals!" * leeN (~leeN-at-g227206094.adsl.alicedsl.de) has joined ##c++ * Wizfarm (~Wizfarm-at-50.243.244.110) has joined ##c++ * pndemc (~pndemc-at-192-171-41-68.cpe.pppoe.ca) has joined ##c++ * zeitue has quit (Ping timeout: 240 seconds) * kez_ has quit (Remote host closed the connection) I'll take the long answer, what did I do wrong exactly? http://ideone.com/m7Hv9a friend handed me this nice code. the explanation answer is that "root" is defined in two translation units and then the linker doens't know what to do with that because it expects only one definition + * Nurbs (~nurbs-at-2a02:908:eb11:4380:224:8cff:fe08:8012) has joined ##c++ Silex, right so I tried to eliminate it from the other .o file and it still did this * homovitruvius has quit (Ping timeout: 250 seconds) * zenith_ (~zenith-at-user3-81-57.wireless.utoronto.ca) has joined ##c++ * amcgee7 (~amcgee7-at-38.104.105.146) has joined ##c++ I would think if I define it in the .h file and only use it elsewhere I shouldn't have this problem * AmR|EiSa_ has quit (Ping timeout: 244 seconds) it's defined in your header, which is included in several .cpp... so in the end it's defined in several .cpp * redpill (~redpill-at-unaffiliated/redpill) has joined ##c++ ah * bitlord has quit (Quit: leaving) Erlkoenig: +1 * Galik (~galik-at-unaffiliated/galik) has joined ##c++ Boy I'm lucky you are hear this morning why is this not a problem though for structs also defined in the .h mrbrklyn: the right thing is to remove it completely from your header, move "bnode* root" inside your main() and pass that to all the fucntion needing it because they are defined and not declared... no structs are declared I'll do that, I just want to not just fix the problem ;) I wanted to understand it
http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration (at stackoverflow.com) they are declared and not defined.. * wasanzy has quit (Ping timeout: 246 seconds) * FLHerne (~flh-at-unaffiliated/flherne) has joined ##c++
|
|