473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

return type unknown for template classes

I have created a matrix class and want to add two matrices of different data types. Like for int and double return type of matrice should be double. How can I do that???
This is my code
Expand|Select|Wrap|Line Numbers
  1. template<class X>
  2. class Matrix
  3. {
  4. ..........
  5. .........
  6. template<class U>
  7. Matrix<something> operator+(Matrix<U> &B)
  8. {
  9.     if((typeid(a).before(typeid(B.a))))
  10.         Matrix<typeof(B.a)> res(1,1);
  11.     else
  12.         Matrix<typeof(a)> res(1,1);
  13. }
What should be something here??? Also what should be done so that I can use "res" outside if else statement???
Apr 25 '13 #1
1 1599
weaknessforcats
9,208 Expert Mod 8TB
If you are using two different types to ceate and work with a Matrix, then you need both types as template arguments:

Expand|Select|Wrap|Line Numbers
  1. template<class T, class U>
  2. class Matrix
  3. {
  4. public:
  5.     template<class T, class U>
  6.     Matrix<T,U> operator+(Matrix<T,U>& rhs)
  7.     {
  8.     Matrix<T,U> result;
  9.  
  10.     return result;
  11.     }
  12. };
  13.  
In addition, to use your res object outside the "if" statement, you need to create it on the heap and delete it before you leave the Matrix<T,U>::operator+.
Apr 26 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
2
by: Xav | last post by:
#include <iostream> using namespace std; class CLog { public: template<typename paramT> CLog& operator<<(const paramT& param) {
1
by: Prakash Bande | last post by:
Hi, I have a template class as below: template <class _T> class myclass{ public: myclass(){ obj = new _T();} _T* obj; _T* operator->() (return obj;} };
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
1
by: Dave Booker | last post by:
Is there any way to change the return types on a member function in derived classes? E.g., something like: public abstract class A { protected abstract Object f(); } public abstract class...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those classes. For example, let's say I send an update from...
7
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
2
by: Filip Konvicka | last post by:
Hi all, I wonder whether anyone has seen a workaround for this somewhere, perhaps in Boost or elsewhere... I succeeded using a local type as template argument in MSVC 8.0, but I found out...
5
by: Daniel Pitts | last post by:
I have a template: template<typename t, int foo> class MyTemplate { public: template<typename bar> MyTemplate() { bar::something(foo); } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.