Take the 2-minute tour ×

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..

share|improve this question
Where have you implemented RegMatrix::getCol() const ? –  Charles Bailey Oct 3 '10 at 11:15
Could you provide the commands or method you used to compile your code? –  JoshD Oct 3 '10 at 11:17
Check if you have given the proper paths for your libraries ? Your code has compiled fine. –  DumbCoder Oct 3 '10 at 11:19
1. I've implemented RegMatrix::getCol() const inside RegMatrix.cpp file. 2. I've compiled using Visual Studio (2010). In linux, i've compiled with the line: "g++ -Wall *.cpp *.h (all the necessary files) -o Matrix" –  darven Oct 3 '10 at 11:33
Your error is clearly from Visual Studio. Are you including RegMatrix.cpp and SparseMatrix.cpp in the same Visual Studio project? –  Charles Bailey Oct 3 '10 at 11:45
add comment

2 Answers

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?
share|improve this answer
add comment

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:


class RegMatrix {
 // stuff, no mention of SparseMatrix


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) { .. }
share|improve this answer
add comment

Your Answer


