Hi I'm afraid that the following code is giving me a "undefined reference to 'strSet::strSet(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'"
The following is a header file for an object strSet which stores a vector of strings.
Although I've clearly defined both constructors in the header file and respective cpp file, it tells me that the constructor is undefined. Is there any particular reason why?
Thanks for the reply, unfortunately I have no control over changing the constructor parameters since I am not allowed to change the header file.
However changing it to tempSet = new strSet(string("hello")); does not fix the problem.
To be more precise this is not the only error I am getting since I also get a similar "undefined reference" error in my struct definitions:
/tmp/ccuWFpZX.o: In function 'eleOfSet::eleOfSet()':
setcalc.cpp:(.text._ZN8eleOfSetC1Ev[eleOfSet::eleOfSet()] + 0x27): undefined reference to 'strSet::strSet()'
LOL thank you very much.
I changed #include "strset.h" with #include "strset.cpp" and it worked fine. My apologies I just don't entirely understand the linking step from the compiler.
When I said #include "strset.h" doesn't it just refer to the header functions, and subsequently find a .cpp file where they are defined?
The reason why I wrote this is because I know it is customary to write #include <string.h> to use strcmp function. So when is it useful to include headerfiles as aposed to .cpp files?
No, don't do that. Linking is done by passing parameters to the compiler, not by inclusion. You do NOT include cpp files. (well, you can, but that defeats their entire point).
I had this exact same problem. The solution was that I forgot to set a target for my CPP file when linking it to the project. What compiler are you using?
g++ -c file1.cpp #creates file1.o
g++ -c file2.cpp #creates file2.o
g++ file1.o file2.o -o program # links file1.o and file2.o together to form the executable program
#You can also write it like this
g++ file1.cpp file2.cpp -o program
Basically, this is done to be able to spread your code on multiple files so you don't have to recompile the entire program again after changing just one line... For example, if you have file1.cpp and file2.cpp, you would compile them to get file1.o and file2.o. Linking them gives you the executable. Now if you change file1.cpp you only need to recompile file1.cpp, and link the new file1.o to the old file2.o to create the new executable. Header files are there to provide the interface to an object file, basically so the compiler knows what symbols he can use.