I have a header file that I've saved methods I use in nearly every program I write in. In the past this header file was mainly relocated just to my main() cpp file. For my most recent program however. This time when I go to compile I get a LNK2005 error, for each of my methods. Other than copy-n-paste-ing my code into each class I'm writing, how do I solve this issue?
-The header file has #pragma once as do each of my classes
1 2
Error 1 error LNK2005: "void __cdecl getString(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?getString@@YAXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Address.obj c:\Users\Duke\documents\visual studio 2013\Projects\AcmeAnvil\AcmeAnvil\DriverApp.obj AcmeAnvil
If you include the implementation of functions in your header file and that header gets included in multiple .cpp files, you're going to get linker errors indicating the those functions have been defined multiple times.
You should include only the function prototypes for those functions in your header file and then include the implementation of those functions one and only one .cpp file.
I've included the .h in the base class, which then get's included in the next class. Thus the .h is only explicitly included in one class. The .h is a stand-alone file, with all code in the .h
The class is fine, it's my .h of methods that is having the issue. I can get the classes to mesh just nicely. But, the methods I use all the time for data validation and such in the customized .h file are the one's I'm having the lnk2005 error on.
the methods I use all the time for data validation and such in the customized .h file are the one's I'm having the lnk2005 error on.
What part of "Do not include class or function implementations in .h files." did you not understand?
The linker error you getting says getString is defined multiple times. i.e. once in Address.obj and once in DrtiverApp.obj. This means that the implementation for getString exists in both Address.cpp and DriverApp.cpp. I can only presume that this is happening because the implementation for getString is in a header file which is included in both Address.cpp and DriverApp.cpp.