SlickEdit Community

General => General Programming => Topic started by: patrickkox on November 17, 2019, 08:41:14 pm

Title: C++ and including header files still give undefined reference error
Post by: patrickkox on November 17, 2019, 08:41:14 pm
Hi all,I'm trying to learn C++ with the book "Professional C++ 4th Edition" by Marc Gregoire, but I'm running into a problem.
The code I am working with is an example for overloading an operator and consists of 3 files:main.cppArray.hArray.cpp
I've included Array.h in both main.cpp and Array.cpp. And if I understand correctly I do not need to add Array.cpp to main.cpp?
But when I do this, I keep getting linker errors:
[SE patrick@debian OverloadingTheSubscriptionOperator]$ echo VSLICKERRORPATH="/home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator/"
[SE patrick@debian OverloadingTheSubscriptionOperator]$ "/home/patrick/slickedit-professional/bin/vsbuild" "/home/patrick/projects/Professional_C++/Part_3/Chapter_15/Chapter_15.vpw" "/home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator/OverloadingTheSubscriptionOperator.vpj" -t rebuild -signal 40239
---------- 'rebuild' Project: 'OverloadingTheSubscriptionOperator/OverloadingTheSubscriptionOperator.vpj' - 'Debug' ---------- VSLICKERRORPATH=/home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator
/usr/bin/ld: Debug/main.o: in function `main':
/home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator//main.cpp:8: undefined reference to `Array<int>::Array()'
/usr/bin/ld: /home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator//main.cpp:11: undefined reference to `Array<int>::setElementAt(unsigned long, int const&)'
/usr/bin/ld: /home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator//main.cpp:16: undefined reference to `Array<int>::getElementAt(unsigned long) const'
/usr/bin/ld: /home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator//main.cpp:8: undefined reference to `Array<int>::~Array()'
/usr/bin/ld: /home/patrick/projects/Professional_C++/Part_3/Chapter_15/OverloadingTheSubscriptionOperator//main.cpp:8: undefined reference to `Array<int>::~Array()'
collect2: error: ld returned 1 exit status
*** Errors occurred during this build ***
[SE patrick@debian OverloadingTheSubscriptionOperator]$

this error goes away when I add an #include for Array.cpp to main.cpp, but that shouldn't be required right?
(this also happens when I try to manually compile with $ g++ -oMain main.cpp Array.cpp (or g++ -oMain main.cpp Array.h Array.cpp, but that gives an error that there is a #pragma once in the main.cpp, but I also tried replacing #pragma once with the ifndef method with the same results).

Now I just tested with a simple Hello World program where I also have 3 files:main.cpp which calls a function called "sayHello()hello.h which contains the function prototype for sayHello()hello.cpp which contains the implementation for sayHello()
and this works without any problems!
so what am I doing wrong? I'm guessing it's not a SlickEdit problem since it also happens when using gcc from the command line.but in-case it matters, I'm using all default options with the new project wizard (so without any makefiles).
Title: Re: C++ and including header files still give undefined reference error
Post by: patrick on November 18, 2019, 02:25:29 pm
I took a quick look at that source from the book, and that Array.cpp contains a template.  So you would need to #include it.  Might want to check back in the templates chapter, and see if they mention this explicitly.  There may be a convention they follow in the book.

Our build system just sees it as another cpp file, and so compiles it, because that's the rule for .cpp files.  Which isn't helpful in this case, but we can't tell how the file was intended to be used.  In our own source, when we have a template that doesn't go into a header, but a source file so we can control where/how it's instantiated, we give it another extension, like "Something.inl", so the build system won't try to g++ -c it.
Title: Re: C++ and including header files still give undefined reference error
Post by: patrickkox on November 18, 2019, 10:50:27 pm
Thanks for the reply!
The book omits a lot of code for brevity. For example they don't include the #include directives or using namespace std; in most of the code examples in the book.
In this case they just mention definition and implementation. Not actual files. I guess it was confused with "normal classes" where they use a .h and .cpp file. I will have to look back in the book, but I think they just put both in the same .h file.