views:

337

answers:

2

I apologise for what I'm pretty sure is a fairly stupid question, but I can't get it to work!

I'm also not sure what information is too much information so I probably won't give enough info so sorry for that too - just ask.

I began writing a class within main.cpp, and it got large so I decided to shift it to a different source file. I'm not too sure on how to do this, and can't figure anything to help fix this specific problem from internet resources (hence the asking).

I started off with the class definition including all of the function definitions above the main program function. This runs fine. I then split this up into two separate pieces. The class declaration (I think that's the correct term) above the main function and the function definitions below the main function.

This also runs perfectly well. I proceeded to cut the class declaration out into a header file. This header file is of the form

#ifndef INC_MATRIX_H
#define INC_MATRIX_H
class matrix{
//ETC
};
#endif

Which I have read somewhere is useful but I'm not entirely sure why, I think it's to stop redeclaration of the functions if the header is included more than once.

So currently we have this header file included along with the other includes. Then the main function and then the function definitions beneath the main function. This also compiles and runs perfectly well.

The next step I took was to cut the function definitions into their own separate .cpp file. The only addition that was made to this .cpp file was that some extra includes had to be added to the top (specifically iostream and cstdlib). ALSO the matrix.h file was included.

In this configuration Dev-C++ brings up linker errors when I try to compile and run the code. Specifically they are of the form

[Linker Error] undefined reference to matrix <bool>::matrix(int, int)

And the code doesn't run (obviously). how can I fix this? Thanks in advance.

Edit: It's been discovered that this is due to the fact that it's a templated class and in the scope of the matrix.cpp file the template isn't introduced to the bool type. I now want to figure out how to fix this without adding lots of lines of code to individually make each function accept each given type. Oh, and I appreciate that I can define the functions in the header. But I thought that we weren't meant to do that? I thought the idea was that you simply include the declaration.

+1  A: 

I think you probably didn't add matrix.cpp to your project. It has to build that to matrix.o and link it to main.o to create your .exe.

Warren Young
+1  A: 

The error suggests that your matrix class is a templated class. Is it? Perhaps posting the code would help.

If it is a templated class then see this FAQ for a description of the general problem with separating templated classes into header/implementation, and solutions to this problem.

drspod
:). Thanks, it's definitely a template problem. having read the first FAQ I completely understand why it doesn't work. As far as I can tell it goes through the code looking for implementations of your class with whatever type it can find for (in my case) T, and then compiles specific versions of the class for that type for T. It's kind of like automatic overloading. Cool. Now to figure out how to fix it.
VolatileStorm
Okay after reading the rest of that FAQ I was wondering if there is a better way around it? All of the functions are defined in like an individual template scope. I.e. for each function (predeclared in the header) they all have template <typename T>sitting before them. This is kinda okay, even if it was a bit brute forcey to do it. however this causes problems for if I have to add code along the lines of: template matrix<bool>::matrix<bool>(int, int);As I'll have to add one for each function AND for each data type. Is there a way to do this that is shorter?
VolatileStorm