Thread: Template Class and C2059 Error

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    5

    Template Class and C2059 Error

    I am making a matrix template class it is declared as follows:

    Code:
    template <typename T> 
    class CMatrix
    {
    private:
    vector<vector<T> > matrix;
    int numCols;
    int numRows;
    
    public:
    	CMatrix(void)
    	{
    	  numCols=1;
    	  numRows=1;
    	  matrix.resize(numRows);
    	  matrix[1].resize(numCols);
    	}
    
    	CMatrix(int nCols,int nRows);//to initialize a Matrix
    	~CMatrix(void);
    	int GetNumCols();
    	int GetNumRows();
    	void SetNumCols();
    	void SetNumRows();
    	int InsertColumn(vector<T> col);
    	int InsertRow(vector<T> row);
    	int InsertColumnAt(vector<T> col, int index);
    	int InsertRowAt(vector<T> row, int index);
    	vector<T> RemoveColumnAt(int index);
    	vector<T> RemoveRowAt(int index);
    	T GetElementAt(int row, int col);
    	int SetElementAt(T element, int row, int col);
    	int AddMatrix(CMatrix<T> m);
    	int SubtractMatrix(CMatrix<T> m);
    	int MultiplyMatrix(CMatrix<T> m);
    	int DivideMatrix(CMatrix<T> m);
    	int AddScalar(int scalar);
    	int SubtractScalar(int scalar);
    	int	MultiplyScalar(int scalar);
    	int DivideScalar(int scalar);
    	int RotateX(int angle);
    	int RotateY(int angle);
    	int RotateZ(int angle);
    	int Transpose();
    	int LeastSquares();
    	int Inverse();
    
    
    	
    };
    
    The function I have problems with is the constructor that takes 2 integer parameters to define the size of the matrix:
    
    template <typename T>
    CMatrix<T>::CMatrix(int nCols, int nRows)
    {//create a new matrix of size nRows x nCols
    	numCols=nCols;
    	numRows=nRows;
    	matrix.resize(numRows);
    	
    	for(int i=0; i<numRows;i++){
    		matrix[i].resize(numCols);
    	}
    }
    When I declare a variable of type CMatrix and use this constructor:

    Code:
    CMatrix <int> test(5,10);
    I get the compiler error C2059: syntax error: 'constant'
    I am using Visual C .Net

    I can't find any explanation for this specific C2059 error. Any help would be appreciated.

    Thanks.

    Sara

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    CMatrix(void)
    {
        numCols=1;
        numRows=1;
        matrix.resize(numRows);
        matrix[1].resize(numCols);
    }
    One issue... that should be a 0 and not a 1. Don't know if that's a factor with your problem though... don't have Visual C .Net in front of me at the moment.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Thanks for pointing that out. But no, that wasn't what was causing the compiling error

    Sara

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Are you putting the implementation code in a seperate CPP file?

    Trim the class down to nothing but constructors. If it still doesn't compile, post the code so that others can compile it and reproduce the issue.

    gg

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    5
    Hi,

    The code is all in one file

    Code:
    #pragma once
    
    #include <vector>
    using namespace std;
    
    
    template <typename T> 
    class CMatrix
    {
    private:
    vector<vector<T> > matrix;
    int numCols;
    int numRows;
    
    public:
    	CMatrix(void);
    	CMatrix(int nCols,int nRows);//to initialize a Matrix
    	~CMatrix(void);
    	
    
    
    	
    };
    
    
    template <typename T>
    CMatrix<T>::CMatrix(void)
    {
    	 numCols=0;
    	 numRows=0;
    }
    
    template <typename T>
    CMatrix<T>::CMatrix(int nCols, int nRows)
    {//create a new matrix of size nRows x nCols
    	numCols=nCols;
    	numRows=nRows;
    	matrix.resize(numCols);
    	
    	for(int i=0; i<numCols;i++){
    		matrix[i].resize(numRows);
    	}
    }
    
    template <typename T>
    CMatrix<T>::~CMatrix(void)
    {
    
    }
    Here is the stripped down version

    Thanks

    Sara

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
     
    int main()
    {
    //create matrix of default row and column
    vector< vector<int> > mat;
     
    int x = 5;
    int y = 6;
     
    //resize to x rows by default column matrix
    mat.resize(x);
     
    //resize to x row by y column
    for(int i = 0; i < x; ++i)
    	mat[i].resize(y);
     
    return 0;
    }
    This compiles for me. Try switching numCols and numRow in your two parameter constructor, though that shouldn't prevent compilation.
    Last edited by elad; 08-16-2004 at 01:08 PM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <vector>
    using namespace std;
     
    template <typename T> 
    class CMatrix
    {
    private:
    vector<vector<T> > matrix;
    int numCols;
    int numRows;
     
    public:
    CMatrix(void);
    CMatrix(int nCols,int nRows);//to initialize a Matrix
    ~CMatrix(void); 
    };
     
     
    template <typename T>
    CMatrix<T>::CMatrix(void)
    {
    numCols=0;
    numRows=0;
    }
     
    template <typename T>
    CMatrix<T>::CMatrix(int nCols, int nRows)
    {//create a new matrix of size nRows x nCols
    numCols=nCols;
    numRows=nRows;
    matrix.resize(numCols);
     
    for(int i=0; i<numCols;i++){
    matrix[i].resize(numRows);
    }
    }
     
    template <typename T>
    CMatrix<T>::~CMatrix(void)
    {
     
    }
     
    int main()
    {
    int x = 5;
    int y = 6;
     
    CMatrix<int> mat(x, y);
     
    return 0;
    }
    this compiles for me, too. I can't use the [] operators to access elements of CMatrix because they haven't been overloaded yet, but that wasn't the question.
    Last edited by elad; 08-16-2004 at 01:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  5. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM