Question:
C++ "multiple definitions of"?
2010-06-29 17:21:02 UTC
I am including this header file in 2 files: the class implementation (ImageManager.cpp) and main.cpp.
ImageManager.h
http://pastebin.com/raw.php?i=QC6Kfv7A

I am getting this error in g++

/tmp/cccDqnuy.o:(.bss+0x0): multiple definition of `ImageManager'
/tmp/cc3mk2PM.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

If I remove the object declaration at the end of ImageManager.h ( } ImageManager; ), it works fine. However I'd like to include it there. I've tried changing the object declaratino to manager or imageManager, alas, I get the same error.
Three answers:
Ratchetr
2010-06-29 17:44:47 UTC
Each module that includes ImageManager.h will declare a global variable named ImageManager of type class ImageManager.



So main.cpp says: Hey, I've got a global variable named ImageManager, and everyone can muck with it.

And ImageManager.cpp says: Hey, I've got a global variable named ImageManager and everyone can muck with it.



The linker says: Whoa....main and ImageManager both have variables named ImageManager. I can't be sure they are talking about the same thing here, so I'm not going to guess and risk guessing wrong. Barf.



There are a couple ways to fix it:

1) In the .h file, make ImageManager (the variable) an extern:

extern class ImageManager ImageManager;

Then, in ImageManager.cpp, declare the actual instance:

class ImageManager ImageManager;



2) Use the singleton pattern: Give the ImageManager class a public static function that returns your one and only ImageManager object. You would declare that in ImageManager.cpp as static class ImageManager ImageManager;. This method would just return ImageManager;



2 is cleaner than 1, Global variables are bad, in general. But takes a bit more work to implement.



Side note: My C++ is a bit rusty, haven't used it for 3 years. So I may be a tad off on the syntax above, but it should be close.



Side note 2: When I first moved from C++ (a language I used to love) to C# (my current love), I really thought I would miss header files. They seemed like such a good idea at the time. I don't. Header files are something they used back in the dark ages. There's no need for them today.



ETA: I wrote Factory pattern in my original post. Brain fart wrong. Edited to say Singleton pattern as it should.
Cubbi
2010-06-29 19:58:02 UTC
You're defining identically named objects with static duration and external linkage in every translation unit that includes this .h file, which is why no two such translation units can be linked together. The include guards "the whole ifdef/define/endif" do something different.

It is also bad style to define a class and an object of that class type in a single statement.



Explain why do you want to "include it there" to see how it can be done in C++. Typical approaches, are: 1) instantiate an object of this type with auto or dynamic duration and access it by reference/smart pointer where needed 2) instantiate an object of this type with static duration in *one* translation unit and use the extern declaration in the other units that must access it, and 3) make it a Singleton and access it via static function such as ImageManager::getInstance()



By the way, I have a feeling that ImageManager::isImageUsed() and ImageManager::getImage() should be declared const, especially seeing how the latter returns a const reference. Do they modify ImageManager while obtaining that bool or that reference?
2016-04-13 00:49:24 UTC
1. Cults reject all paradoxes of Scripture. 2. Cults claim to have new truths. 3. Cults reject anything they cannot explain. 4. Cults do not rightly divide the Word of God. 5. Cults use the Bible…plus! 6. Cults preach another Jesus. 7. Cults reject orthodox Christianity. 8. Non-biblical teaching on the nature of God. 9. Cults follow changing theology 10. Cults recognize a man or group as having absolute authority. 11. Cults deny salvation by faith alone. 12. Cults often promulgate false prophecy. 13. Cult leaders do not want their followers to think for themselves. 14. Cults use questionable conversion tactics (they use leading questions as a means to identify potential converts. They seek to discover those who have fears about the condition of the world, those who are lonely, those who have gripes against the establishment, or those who are weak and vulnerable due to transitional situations in life). 15. Cults promote pride of the group/exclusivity/separatism. 16. Cultists make it difficult to leave the organization. 17. Cults use subtle brainwashing methods (New converts are indoctrinated into the cult’s belief system incrementally, and they are separated from family and society…not physically, but mentally).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...