I need your help,Thank u.

This is a discussion on I need your help,Thank u. within the C++ Programming forums, part of the General Programming Boards category; Hi I have written a class with the name of Matrix which should work like sum,.. on matrixes,and becouse it's ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    In my mind
    Posts
    1

    Smile I need your help,Thank u.

    Hi

    I have written a class with the name of Matrix which should work like sum,.. on matrixes,and becouse it's type is not clear i have used 2 template(one for the type of my matrix,and one for which should be summed to mine) but it sais to me that your decleration is not match to my definition,i don't know why?

    here it is:
    Code:
    in matrix.h:
    
    class Matrixes
    {
    private:
    matrix_type mat[10][10];
    int row,column;
    
    public:
    
    template<class T>
       Matrixes sum(Matrixes<T>);
    };
    
    
    in matrix.cpp:
    
    
    template<class matrix_type,class T>
    Matrixes Matrixes<matrix_type>::sum(Matrixes<T>mat2())
    {
    	
    	Matrixes<T> result(row,column);
    	
    	if(mat2.get_column()!=get_column() || mat2.get_row()!=get_row())
    		cout<<"They can not be added together"<<endl;
    	else
    		for(int i=0;i<row;i++)
    			for(int j=0;j<column;j++)
    				result.get_mat()[i][j]=static_cast<T>((mat2.get_mat()[i][j]+mat[i][j]));
    
    	return result;
    
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You've got templates all wrong. There are just so many things wrong in this code, regarding templates, that I suggest you either not use them or you read a tutorial on them. I know this sounds mean but it is the raw truth. Sorry.

    PS: Also, don't you think overloading the + operator is better than having a sum() function ?

    Edit: I also encourage you strongly to read the last point in this thread.

    http://cboard.cprogramming.com/annou...t.php?f=3&a=51
    Last edited by Desolation; 05-10-2007 at 11:46 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    A good technique when writing code you want to be templated is to write it first with a regular type, then when you've got it working, try changing it to a template.

    >> Matrixes Matrixes<matrix_type>::sum(Matrixes<T>mat2())
    The empty parentheses at the end of this line shouldn't be there, right?

    >> Also, don't you think overloading the + operator is better than having a sum() function ?
    I'd say it's fine to start with a sum function, since an overloaded operator is just a fancier version of that.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    It is more intuitive though to use the + operator than to use a function.

    PS: I don't want anyone picking me up on the fact that an operator *is* a function as well -.-

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21