MESSAGE
DATE | 2015-02-20 |
FROM | Ruben Safir
|
SUBJECT | Subject: [LIU Comp Sci] a little C++ insite
|
From owner-learn-outgoing-at-mrbrklyn.com Fri Feb 20 10:27:46 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 53DE716130E; Fri, 20 Feb 2015 10:27:46 -0500 (EST) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id 4227616130A; Fri, 20 Feb 2015 10:27:46 -0500 (EST) Delivered-To: learn-at-nylxs.com Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mrbrklyn.com (Postfix) with ESMTP id 7ACC9161307; Fri, 20 Feb 2015 10:27:44 -0500 (EST) Received: from [10.0.0.19] (unknown [96.57.23.82]) by mailbackend.panix.com (Postfix) with ESMTPSA id B729B123FA; Fri, 20 Feb 2015 10:27:41 -0500 (EST) Message-ID: <54E7526C.7090809-at-panix.com> Date: Fri, 20 Feb 2015 10:27:40 -0500 From: Ruben Safir User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: hangout , learn-at-nylxs.com Subject: [LIU Comp Sci] a little C++ insite Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
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++
|
|