1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

LNK2019 Error

Discussion in 'C++' started by Petey_Pii, Feb 23, 2010.

  1. Petey_Pii

    Petey_Pii New Member

    Joined:
    Feb 23, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    so im learning C++ and i wanted to try creating a template class and declare it in a header file and define it in a cpp file. Easy right? Until now, yes. I have solved all but what seems to be the final problem in my code. It gives me "error LNK2019: unresolved external function" and im pretty sure it is because it is not finding the function declarations in the class cpp. I know this because i pasted the code from the class cpp into the main cpp and it compiled and ran fine but i want to do this properly. Im running Microsoft Visual C++ 2008. Ive checked that all files are included in the project. Ill show you my code and i hope someone can help

    Matrix.h:
    Code:
    #ifndef MATRIX_H
    #define MATRIX_H
    
    template <typename T>
    
    class Matrix
    {
    private:
    	int mGridWidth;
    	int mGridHeight;
    	T *mGrid;
    
    public:
    	Matrix(int gridWidth, int gridHeight);
    	~Matrix(void);
    	T& operator ()(int column, int row);
    	int GetWidth();
    	int GetHeight();
    };
    
    #endif
    Matrix.cpp:
    Code:
    #include "Matrix.h"
    
    template <typename T>
    Matrix<T>::Matrix(int gridWidth, int gridHeight)
    	: mGridWidth(gridWidth), mGridHeight(gridHeight)
    {
    	mGrid = new T [mGridWidth * mGridHeight];
    }
    
    template <typename T>
    Matrix<T>::~Matrix(void)
    {
    	delete[] mGrid;
    }
    
    template <typename T>
    T& Matrix<T>::operator ()(int column, int row)
    {
    	return mGrid[column * mGridWidth + row];
    }
    
    template <typename T>
    int Matrix<T>::GetWidth()
    {
    	return mGridWidth;
    }
    
    template <typename T>
    int Matrix<T>::GetHeight()
    {
    	return mGridHeight;
    }
    main.cpp:
    Code:
    #include <iostream>
    #include "Matrix.h"
    
    int main()
    {
    	using namespace std;
    
    	Matrix<double> Foo(8,8); //this line of code causes the error
    
    	cout << "Press Enter to continue...";
    	cin.get();
    
    	return 0;
    }
    i hope this is enough information if you need anything else just ask and thx in advance.
     

Share This Page