LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2005, 08:21 AM   #1
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Type conversion problem- C++


Hi to all.

My problem lies in this statement
Code:
Weight=Weight+x;
where Matrix<float> Weight;
Matrix<int> x;

I have tried things like
Code:
Weight=Weight+(Matrix<float>) x;
Type cast:
operator Matrix<float> ()
	{
		Matrix<float> x(rows,columns);
		for(int i=0;i<rows;i++)
			for(int j=0;j<columns;j++)
				x.Array[i][j]=Array[i][j];
		return x;
	}

// not sure about the correctness of this thing
Please help me. Expecting quick responses.
Thanks to all the hearts that care to help me.
 
Old 12-29-2005, 08:31 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I take it you have overloaded the addition and assignment operators.
 
Old 12-29-2005, 09:38 AM   #3
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
yes i have...
Code:
template <class Type>
Matrix<Type> :: Matrix(Matrix &matrix)
{
	rows = matrix.rows;
	columns = matrix.columns;
	Array=new Type* [rows];
	for(int i=0;i<rows;i++)
		Array[i]=new Type[columns];
	for(i=0;i<rows;i++)
		for(int j=0;j<columns;j++)
			Array[i][j]=matrix.Array[i][j];
}

template<class Type>
void Matrix<Type> :: operator =(Matrix<Type> &matrix)
{
	if(rows != matrix.rows || columns != matrix.columns)
	{
		cerr<<" Order mismatch";
		exit(1);
	}

	for(int i=0;i<rows;i++)
		for(int j=0;j<columns;j++)
			Array[i][j]=matrix.Array[i][j];
}

template<class Type>
Matrix<Type> operator + (Matrix<Type> &matrix)
{
	if(rows != matrix.rows || columns != matrix.columns)
	{
		cerr<<" Order mismatch";
		exit(1);
	}
	
	Matrix<Type> x(rows,columns);
	for(int i=0;i<rows;i++)
		for(int j=0;j<columns;j++)
			Array[i][j]=Array[i][j]+matrix.Array[i][j];
    return x;
}
------
Edit:
Error:
'type cast' : cannot convert from 'class Matrix<int>' to 'class Matrix<float>'
No constructor could take the source type, or constructor overload resolution was ambiguous


wonder why?

Last edited by vivekr; 12-29-2005 at 09:40 AM.
 
Old 12-29-2005, 11:54 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
im not sure if im going along the correct lines of thought here but instead of
Code:
Weight=Weight+x;
where Matrix<float> Weight;
Matrix<int> x;

what happens when you do
Code:
x=Weight+x;
does that give you an error?

I'm not sure but i think the func
Code:
template<class Type>Matrix<Type> operator + (Matrix<Type> &matrix)
is returning a matrix of type int if the parameter is of type int.

you may need to do specialisation such as
Code:
(Matrix<int> &matrix)
then again i could be completly wrong

Last edited by dmail; 12-29-2005 at 12:01 PM.
 
Old 12-29-2005, 07:00 PM   #5
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
Code:
template<class Type1,class Type2>
Matrix<Type1> Matrix<Type1>::operator + (Matrix<Type2> &matrix)
{
	if(rows != matrix.rows || columns != matrix.columns)
	{
		cerr<<" Order mismatch";
		exit(1);
	}
	
	Matrix<Type2> x(rows,columns);
	for(int i=0;i<rows;i++)
		for(int j=0;j<columns;j++)
			Array[i][j]=Array[i][j]+Type1(matrix.Array[i][j]);
    return x;
}
This pops up template function has already been defined
error.

As u said I tried to be specific by overloading for the float and int types alone.that too resulted in the same error.

And x=x+Weight -- too gave the same error.
I would love to send u the headerfile containing these operations if u want.
Please help...
Thanks to dmail for ur sincere contribution to sort me out of this...
 
Old 12-30-2005, 08:24 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by vivekr
I would love to send u the headerfile containing these operations if u want.
sure bounce them over and i'll try and help
Code:
dmail00 at gmail.com
 
Old 12-31-2005, 03:47 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try it this way:
Code:
template <class Type1>
struct Matrix
{
        template <class Type2>
                Matrix
        operator + (const Matrix <Type2>&);

        //...
};

template <class Type1> template <class Type2>
Matrix <Type1> Matrix <Type1> ::operator + (const Matrix <Type2> &matrix)
{
        //...
}
You have to separate the template declarations because the first is to open the scope of Matrix, and the second takes place within that scope.

Also, you should make your copy constructor, assignment operator, and + operator take a const Matrix; often times you will be given a const reference to copy, but if your operator doesn't take const it won't work. In other words, take references as const unless you can't do it otherwise.
ta0kira
 
Old 01-02-2006, 10:10 AM   #8
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Original Poster
Rep: Reputation: 15
To ta0kira:
Your idea isn't working..
The second template definition is shown as an error
Code:
template <class Type>
class Matrix
{
....
template<class Type2>
Matrix<Type2> operator *  (Type2 value);
....
};

template<class Type> 
template<class Type2> 
Matrix<Type2> Matrix<Type> :: operator * (Type2 value)
{
	Matrix<Type2> x(rows,matrix.getColumns());

	for(int i=0;i<rows;i++)
		for(int j=0;j<matrix.columns;j++)
			x.Array[i][j]=Array[i][j]*value;		
	return x;
}

 Errors:
syntax error : ''template<''
'Type2' : undeclared identifier ....
template definitions cannot nest
To dmail:
Sure I will send my code by tomo.
I actually was discussing a situation where a type cast wasnt required. anyway i have found a problem and need to find a soln. So i will send it to u
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion problem spank Programming 13 11-14-2005 03:48 AM
Data type conversion in C zaichik Programming 6 09-10-2005 05:47 PM
c + type conversion dilberim82 Programming 8 03-08-2005 12:17 PM
conversion to non-const reference type max2878 Linux - General 2 05-23-2002 01:25 PM
a question on type conversion in C++ ReverseLogic Programming 3 11-27-2001 11:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration