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/"
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
Array.cpp
main.cpp
Linking...
/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).