tags:

views:

77

answers:

2

Hi, I'm writing a Matrix program: I have a class to represent a Regular matrix (RegMatrix), and a class to represent a sparse matrix (SparseMatrix), that is: represent only the none-zero's values.

In the begining of each H file, i write the opposite class declaration. For example, in SparseMatrix.h I write class RegMatrix (so that the compiler would recognize my refers to this class). In each cpp file i include both of the H files.

My program compiles, but i have loads of linkage errors, saying "unresolved external symbol.."

For example: unresolved external symbol "public: int__thiscall RegMatrix::getCol(void)const " (?getCol@RegMatrix@@QBEHXZ)" [file: SparseMatrix.obj]

I'm going nuts trying to figure out what's wrong with my code..

A: 

Basically, that means something in your program is using getCol from the RegMatrix class, but your compilation does not include the body for such a routine.

Things to look for:

  • Did you actually create an implementation of that routine? With that exact same parameter profile?
  • Did you forget to add the source file that compiles that routine to your list of source files to be compiled and linked into your program? (In VisualStudio, did you add it to the project?)
  • Did you perhaps accidentally leave the RegMatrix:: off of the front of the routine name when you were coding it up in your .cpp file?
T.E.D.
A: 

That has nothing to do with the mutual inclusions of header files, although this is not a nice design. You forgot or misstyped the definition (forgot some const qualifier or something like that). Regarding that 'not so nice design' statement: You have apparently interleaved two classes, so that they cannot be use independently. How do you write a unit test for a RegMatrix without interleaving with SparseMatrix, or vice versa? I suggest to make the two definitions completely independent of each other and add a matrix type converter implementation that knows both matrix types. Consider the problems, if you decide to add another matrix types: You will have to modify exponential number of files, to add conversions. To most simplisitic design could look like:

regmatrix.h:

class RegMatrix {
 // stuff, no mention of SparseMatrix
};

sparsematrix.h:

class SparseMatrix {
 // stuff, no mention of RegMatrix
};

conversions.cpp: (with declarations in conversions.h)

#include "sparsematrix.h"
#include "regmatrix.h"
#include "uppertriangular.h" // got it?
//...

namespace Conversions {
 void SparseToRegular(SparseMatrix& pTo, const RegMatrix& pFrom) { .. }
 void RegularToSparse(RegMatrix& pTo, const SparseMatrix& pFrom) { .. }
}  
paul_71