I am working on a template vector class. I have to have both a .h and .cpp file for this (already emailed teacher saying I that many people just put the implementation of template classes all in the same .h file). Anyway, I am running into an issue with overloading the operator =.
Everything seems to work. Can someone explain this to me? Is this the right approach? I assume I need to specify the template type at each stage to indicate that this particular use of the = operator is of type T.
Yes, it looks like you fixed your own problem. If your teacher wants you to keep the implementation in a separate file, then I suggest naming this separate file with an extension like ".ipp" or "inl" instead of ".cpp" to avoid confusion.
Thanks for the reply. I've run into a problem that I want to debug with Code::Blocks (as opposed to either using gedit and gdb or Emacs and gdb). I am able to compile my code fine in Fedora 15, but not when using Code::Blocks (using GCC compiler). I am assuming my problem might be because I am separating a template class into a .h and .cpp file.
error: redefinition of 'MyVector<T>::MyVector()'
error: 'MyVector<T>::MyVector()' previously declared here
Any idea why I am getting the above error messages with Code::Blocks? Any idea why I am able to compile this under Fedora without running into these errors? If you need to entire code, let me know. Thanks for any assistance.
Last edited by Alterah; September 3rd, 2011 at 07:51 PM.
You have a circular inclusion, so I am surprised that you got it "to compile this under Fedora without running into these errors". Remove the #include "MyVector.h" from MyVector.cpp. Remember, MyVector.cpp is not a source file. It is part of a header.
Hmm...it's funny because this is how they want us to do templates. And, when I looked at it some more, it looked like circular inclusion. What I decided to do for debugging purposes is to just throw everything in the header...at least for now. I find it a lot easier to debug with Code::Blocks (or Visual Studio) than with the command line and gdb.
I want you to copy the contents of this .cpp file and paste it into the header file, then delete this .cpp file. You should get the exact same error (other than say, a difference in line number). If you did not, then obviously you treated this .cpp file as a source file by compiling it when I explicitly told you that it was not a source file, but rather part of a header file. This is also why I suggested that you use a file extension other than .cpp for it.
That was it. If I copy the contents of the implementation file to the header file, I don't get the error.
But, leaving the implementation in the MyVector.cpp, I had added the MyVector.cpp file to the project, so it was trying to compile it as a source file. After reading your comment, I removed the MyVector.cpp file from the project and recompiled everything and everything works.
Just for completion and to possibly answer your statement:
"...so I am surprised that you got it 'to compile this under Fedora without running into these errors'."
see my makefile that I use when I build this on the Fedora 15 system:
I am a bit new when it comes to actually making makefiles...and I feel like I've made a mistake when it comes to the above instructions for MyVector.o...but it appears to compile and link everything together.
for( int i = 0; i < 40; i++ )
for( int i = 0; i < 30; i++ )
So, it does use my vector class. I've been lazy and not created test files, but just modifying my main1 code after I complete a function and running through each case of the code to see if my template class behaves as expected.
Last edited by Alterah; September 4th, 2011 at 05:32 PM.