I need to add two parameters . I'm dealing with string/int/double and boolean.
The first three are doing great , meaning the ADD operation between them results in an exact output.However ,when T=bool , I get the operation of a LOGIC OR , and not a LOGIC AND .
The code :
I have 3 classes :
Management
LinkedList
Matrix
1 2 3 4 5 6 7 8 9 10
template <class T>
class Matrix
{
private:
LinkedList<T> *m_list;
// more fields ,irrelevant for the moment //
}
template <class T>
class LinkedList {
private:
class Node // each node represents a column
{
public:
int _row; // rownumber
int _col; // column number
T _value; // Some T
Node *_next; // pointer to next Node
// initializations of Node
};
Node * _head; // the head of the matrix
};
template <class T>
LinkedList<T>* LinkedList<T>::combineLists(LinkedList<T> *inputList)
{
Node* inThis= this->m_head;
Node* inInput= inInput->m_head;
...
...
// same column and same row //
if (inThis->_row == inInput->_row && inThis->_col == inInput->_col)
{
Node * newNode = new Node(inThis->_row , inThis->_col , inThis->_value+inInput->_value); // problematic line
newList->attachNodeToLinkedList(newNode);
}
...
}
As I said above,the program must support 4 kinds of inputs : char,int,double and bool.The first 3 are
working great ,you can see that the operation being done is "inThis->_value+inInput->_value" in
the IF condition above.
In my code ,when I get
0 1 -----> 1
1 0 -----> 1
0 0 ------> 0
1 1 ------> 1
but I need logical AND
1 0 = 0
0 1 = 0
0 0 = 0
1 1 = 1
Without giving any thought to what you are trying to accomplish, I can tell you that you can accomplish it using a template specialization for bool. Check this site's tutorial here: http://www.cplusplus.com/doc/tutorial/templates/.
I can tell you that you can accomplish it using a template specialization for bool
+1 ( Although what I use here techniquely is overloading, template specialization is the way
to go in the general case. Food for thought -> http://www.gotw.ca/publications/mill17.htm )
I would probably do something like this:
1 2 3 4 5 6 7 8 9 10
template <class T>
T AddOp(T left, T right)
{
return left + right;
}
bool AddOp(bool left, bool right)
{
return left && right;
}
And then:
1 2 3 4 5
if (inThis->_row == inInput->_row && inThis->_col == inInput->_col)
{
Node * newNode = new Node(inThis->_row , inThis->_col, AddOp(inThis->_value,inInput->_value));
newList->attachNodeToLinkedList(newNode);
}
If you really want that 'combine' means 'and' use a template especialization
1 2 3 4 5
template <> //now replace every T with bool
LinkedList<bool>* //why are you returning a pointer, instead of an object?
LinkedList<bool>::combineLists(LinkedList<bool> *inputList){
//this is the function to be executed if T==bool
}
Okay ,but can I overload operator + within that template specialization ?
What I need is basically simple , when I have "bool+bool" , I want to do "bool && bool" .
Is this possible ?
thanks a lot!
Don't ask. Test! :-S What is the gain in waiting for a "Yes, you can do it.", or "No, it cannot be done." Test and find out yourself. Don't have a compiler around? I recently learned there are online compilers. I like ideone.com.
can I overload operator + within that template specialization ?
No. Not unless you create a wrapper type around bool. But it's simpler to just define a function.
ne555 wrote:
¿why do you associate + with 'and'?
Well, this is a good question. Usually '+' means 'or' and '*' means 'and'. If you decide to
use this interpretation, you don't have to do anything. (check the thread ne555 suggests)
Remove the template<class T> from the bool version
1 2 3 4 5 6 7 8 9 10 11 12
//specialised or non template version for bool
bool AddOp(bool left, bool right)
{
return left && right;
}
template <class T>
T AddOp(T left, T right)
{
return left + right;
}
C:\Users\T\workspace\CPP05\Debug/../LinkedList.h:90: multiple definition of `addOp(bool, bool)'
main.o:C:\Users\T\workspace\CPP05\Debug/../LinkedList.h:90: first defined here
I don't under what might be the problem ,maybe something to do with consts ?
template <class T>
class LinkedList {
private:
class Node // each node represents a column
{
public:
int _row; // rownumber
int _col; // column number
T _value; // Some T
Node *_next; // pointer to next Node
// initializations of Node
};
Node * _head; // the head of the matrix
};
// here ?? because when I put it here ,I get errors again
template <class T>
class AddOp
{
T operator()(T left, T right) { return left + right; }
};
template <>
class AddOp<bool>
{
booloperator()(bool left, bool right) { return left && right; }
};
template <class T>
LinkedList<T>* LinkedList<T>::combineLists(LinkedList<T> *inputList)
{
Node* inThis= this->m_head;
Node* inInput= inInput->m_head;
...
...
// same column and same row //
Node * newNode = new Node(inThis->_row , inThis->_col, AddOp<T>()(inThis->_value,inInput->_value));
newList->attachNodeToLinkedList(newNode);
...
}
\LinkedList.h:110: error: expected primary-expression before 'template'
..\LinkedList.h:110: error: expected ';' before 'template'
..\main.cpp:22: error: expected '}' at end of input
thank you very much !!